Hi ,

I try to use the getChildNodes, but it return result and then core dump.

Am I doing anything incorrectly?  I really need some help here.  I am
using xerces C++ from the nightly build (9/14) on solaris.
I also attached my full code and data file.  thanks for your help in
advance.


My xml looks like this:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE test SYSTEM "testing.dtd">
<test>
  <CommandIndex  value = "TBD"/>
  <Argument>
        <Record>
                <ID  value = "1"/>
                <Clock  value = "ENABLE"/>
        </Record>
        <Record>
                <ID  value = "1"/>
                <Clock  value = "ENABLE"/>
        </Record>
  </Argument>
</test>


I modifiied the DOMCount examples and added call to getChildNodes:
try
        {
                cout << " parsing buffer .. " << endl;
            parser.parse("testing.xml");

            DOM_Document doc = parser.getDocument();

            DOM_Element root  = doc.getDocumentElement();

            DOM_NodeList demodClock =
root.getElementsByTagName(DOMString("Argument") );

            DOM_NodeList childlist;
            if ( demodClock.getLength() == 1 )
            {
                cout << " Name of doemclock child node: " <<
                (  ((DOM_Element&)(demodClock.item(0))
).getTagName()).transcode()  << endl;

                 childlist = (demodClock.item(0)).getChildNodes();
            }
            else
            {
                cout << " Dup element" << endl;
                return 1;
            }

           cout << " length: " << childlist.getLength() << endl;
        for (int i = 0; i < childlist.getLength() ; i++)
        {
                cout << ( ((DOM_Element&)childlist.item(i)).getTagName()
).transcode() << endl;
        }

***
its prints out this and core dumped
..../bin<9535> ./test
 parsing buffer ..
 Name of doemclock child node: Argument
 length: 5
Segmentation Fault(coredump)

the lenght should be 2 , but it returns 5.
also, Name of doemclock child node should be "Record" .

thanks for your help

Eva
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 rse* are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Xerces" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache\@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation, and was
 * originally based on software copyright (c) 1999, International
 * Business Machines, Inc., http://www.ibm.com .  For more information
 * on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

/*
 * $Id: DOMCount.cpp,v 1.18 2001/08/15 12:41:04 tng Exp $
 */

// ---------------------------------------------------------------------------
//  Includes
// ---------------------------------------------------------------------------
#include <util/PlatformUtils.hpp>
#include <sax/SAXException.hpp>
#include <sax/SAXParseException.hpp>
#include <parsers/DOMParser.hpp>
#include <dom/DOM_DOMException.hpp>
#include "DOMCount.hpp"
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <rw/cstring.h>
#include <rw/tvordvec.h>
#include <rw/rwfile.h>
#include <rw/rstream.h>
#include <framework/MemBufInputSource.hpp>


int main(int argC, char* argV[])
{
    // Initialize the XML4C system
    try
    {
        XMLPlatformUtils::Initialize();
    }

    catch (const XMLException& toCatch)
    {
         cerr << "Error during initialization! :\n"
              << StrX(toCatch.getMessage()) << endl;
         return 1;
    }

    // Instantiate the DOM parser.
    DOMParser parser;
    parser.setValidationScheme(DOMParser::Val_Auto);
    parser.setDoNamespaces(false);
    parser.setDoSchema(false);

    // And create our error handler and install it
    DOMCountErrorHandler errorHandler;
    parser.setErrorHandler(&errorHandler);

        const RWCString dummyId = "prodInfo"; // a dummy sys id that needed by 
MemBufInputSource

        try
        {
                cout << " parsing buffer .. " << endl;
            parser.parse("testing.xml");

            DOM_Document doc = parser.getDocument();

            DOM_Element root  = doc.getDocumentElement();

            DOM_NodeList demodClock = root.getElementsByTagName(DOMString("Argument") 
);

            DOM_NodeList childlist;
            if ( demodClock.getLength() == 1 )
            {
                cout << " Name of doemclock child node: " <<   (  
((DOM_Element&)(demodClock.item(0)) ).getTagName()).transcode()  << endl;
                 childlist = (demodClock.item(0)).getChildNodes();
            }
            else
            {   
                cout << " Dup element" << endl;         
                return 1;
            }

           cout << " length: " << childlist.getLength() << endl;
        for (int i = 0; i < childlist.getLength() ; i++)
        {
                cout << ( ((DOM_Element&)childlist.item(i)).getTagName() ).transcode() 
<< endl;
        }

        } // end try

        catch (const XMLException& toCatch)
        {
                return 1;
        }
        catch (const DOM_DOMException& toCatch)
        {
                return 1;
        }
        catch (...)
        {
                return 1;
        }



    // And call the termination method
    XMLPlatformUtils::Terminate();


}


DOMCountErrorHandler::DOMCountErrorHandler() :

    fSawErrors(false)
{
}

DOMCountErrorHandler::~DOMCountErrorHandler()
{
}


// ---------------------------------------------------------------------------
//  DOMCountHandlers: Overrides of the SAX ErrorHandler interface
// ---------------------------------------------------------------------------
void DOMCountErrorHandler::error(const SAXParseException& e)
{
    fSawErrors = true;
    cerr << "\nError at file " << StrX(e.getSystemId())
         << ", line " << e.getLineNumber()
         << ", char " << e.getColumnNumber()
         << "\n  Message: " << StrX(e.getMessage()) << endl;
}

void DOMCountErrorHandler::fatalError(const SAXParseException& e)
{
    fSawErrors = true;
    cerr << "\nFatal Error at file " << StrX(e.getSystemId())
         << ", line " << e.getLineNumber()
         << ", char " << e.getColumnNumber()
         << "\n  Message: " << StrX(e.getMessage()) << endl;
}

void DOMCountErrorHandler::warning(const SAXParseException& e)
{
    cerr << "\nWarning at file " << StrX(e.getSystemId())
         << ", line " << e.getLineNumber()
         << ", char " << e.getColumnNumber()
         << "\n  Message: " << StrX(e.getMessage()) << endl;
}

void DOMCountErrorHandler::resetErrors()
{
    fSawErrors = false;
}

void preprocess(RWTValOrderedVector <RWCString>& bufferlist,
                 RWTValOrderedVector <RWCString>& msgNamelist)
{
        cout << "enter preprocess.. " <<endl;

        RWTValOrderedVector <RWCString> filelist;

        char fileName[32];
        cout << " please enter name for file: "; 
        cin >> fileName;

        ifstream fin(fileName);
        RWCString data;
        RWCString messageName;

        cout << " Here is the current content of file:"<< fileName << endl;

        cout << " reading file name" << endl;
        char ch;
        // first read xmlfile name into file list
                while(fin.get(ch))
                {
                        if (ch != '\n')
                        {
                                data = data + (RWCString)(ch);
                        }
                        else
                        {
                                cout << "Data:" << data << endl;
                                filelist.insert(data);
                                RWCString clear ;
                                data = clear;

                        }

                }
                fin.close();

        // then read corresponding message name into the messagenamelist

        cout << " REading message" << endl;
        ifstream fmsg ("message");

        char ch2;
        while(fmsg.get(ch2))
        {
             if (ch2 != '\n')
             {
                  messageName = messageName + (RWCString)(ch2);
             }
             else
             {
                    cout << "message:" << messageName << endl;
                    msgNamelist.insert(messageName);
                    RWCString clear ;
                    messageName = clear;

               }

         }
         fmsg.close();

         RWCString qoute = "\"";
         RWCString str1 = "\<?xml version= " + qoute + "1.0" + qoute + " ?" +  "\>" ;

       
        // then for each file name in the filelist, read contents into bufferlist

        RWCString buffer_data;
        cout << " reading buffer" << endl;
        for ( int i = 0; i < filelist.entries(); i ++)
        {
        cout << " in the loop" <<endl;
                RWFile content(filelist[i]);

                // create buffer to hold the file contents
                char* buffer = new char[51079753];
                int len = 51079753;
                // read file content to buffer

                content.Read(buffer);


                buffer_data = buffer;
                delete [] buffer;
                cout << "buffer data.length: " << buffer_data.length() << endl;

                bufferlist.insert(buffer_data);
                RWCString clear;
                buffer_data = clear;

                /*while ( buffer[ len - 2] != 0)
                {
                        buffer = 0;
                        len = len *2;
                        buffer = (char*)realloc(buffer, len*2);
                        cout << " not enough buffer, set len to : " << len << endl;
                        cout << " after RWFile " << endl;

                        content.SeekToBegin();
                        content.Read(buffer);
                        cout << "read the buffer"<<endl;
                }
                 
                std::cout << " buffer: " << buffer << std::endl; */
        }               

}               
        
<!ELEMENT test (CommandIndex, Argument)>
<!ATTLIST test  return     CDATA   #FIXED "void">

<!ELEMENT CommandIndex EMPTY>
<!ATTLIST CommandIndex value      CDATA   #REQUIRED>

<!ELEMENT Argument (Record)+>
<!ATTLIST Argument  datatype   CDATA   #FIXED "Record">

<!ELEMENT Record (ID, Clock)>
<!ATTLIST Record  type  CDATA   #FIXED "struct">

<!ELEMENT ID EMPTY>
<!ATTLIST ID  value      CDATA  #REQUIRED>

<!ELEMENT Clock  EMPTY>
<!ATTLIST Clock  value      (ENABLE | DISABLE) "ENABLE">

<?xml version="1.0" standalone="no"?>
<!DOCTYPE test SYSTEM "testing.dtd">
<test>
  <CommandIndex  value = "TBD"/>
  <Argument>
        <Record>
                <ID  value = "1"/>
                <Clock  value = "ENABLE"/>
        </Record>
        <Record>
                <ID  value = "1"/>
                <Clock  value = "ENABLE"/>
        </Record>
  </Argument>
</test>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to