You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Mirko Braun <tb...@softing.com> on 2002/06/20 20:25:39 UTC

Don't understand the parsing results of the DOMParser

Hi all,

i linked a simple console application projekt (MSVC 6.0 SP 5) with the 
Xerces-C++ 1.7.0 (latest nightly build).
Then i tried to parse the personal.xml file which comes with the whole 
Xerces source zip-file.
First i tried to get the document (root of the document) and wrote its 
childs in a NodeList and get the length ( it counted three).
Then i walked to the last child in this list (e.g. element PERSONELL) and 
wrote its childnodes in a list again and get the
length of this list. I was surprised that the parser found 13 children. I 
expected 6. I got the types of this childrennodes and
the output were the six PERSON of type ELEMENT_NODE (the kind i expected) 
and 7 elements #Text of Type TEXT_NODE
(which had no value).
i can't understand this result. Is something wrong in my Sourcecode (below) 
or i'm just blind?
Cause i'm new to XML, i would be happy if  somebody can help me.

Thanks in advance,

regards, Mirko









   XMLPlatformUtils::Initialize();

   XercesDOMParser *parser = new XercesDOMParser;

   cout << "Generate an  Instance of the XercesDOMParser\n";


     //variable that contains the path of the file
     gXmlFile="C:\\personal.xml";



     //
     //  Parse the XML file, catching any XML exceptions that might propogate
     //  out of it.
     //
     bool errorsOccured = false;
     try
     {
       //start parsing
       parser->parse(gXmlFile);
     }

     catch (const XMLException& e)
     {
         cerr << "An error occurred during parsing\n   Message: "
           << XMLString::transcode(e.getMessage()) << endl;
         errorsOccured = true;
     }

     catch (const DOMException& e)
     {
        cerr << "A DOM error occurred during parsing\n   DOMException code: "
              << e.code << endl;
         errorsOccured = true;
     }

     catch (...)
     {
         cerr << "An error occurred during parsing\n " << endl;
         errorsOccured = true;
     }


   //the return value of the function getDocument() return the root node
   //of the current Document
   DOMNode* doc = parser->getDocument();

   //get the name of the root node
   const XMLCh* rootElementName = doc->getNodeName();

   //copious solution for representation of XMLCh(UTF-16)
   //in the output window
   cout << "Name of the rootelement: " << 
XMLString::transcode(rootElementName);
   cout << endl;



   //doc == root, has root children?
   bool isChildNode = doc->hasChildNodes();

   if (isChildNode)
   {
     cout << "Das Rootelement hat einen Kindknoten\n" ;
   }

   //get the children of the root an list them
   DOMNodeList* childNodes = doc->getChildNodes();


   //get the LastChild of the root (in this case == element personell)
   DOMNode * LastChild = doc->getLastChild();

   //get name of LastChild
   const XMLCh* nameOfLastChildNode = LastChild->getNodeName();

   cout << "This is the Name of the LastChild: " << 
XMLString::transcode(nameOfLastChildNode);
   cout << endl;

   //get all the children of the LastChild and list them
   DOMNodeList* childNodesOfLastChild = LastChild->getChildNodes();

   //get the length of this list, e.g. the number of nodes in the list
   XMLSize_t countCld = childNodesOfLastChild->getLength();

   cout << "Count of the ChildNode of LastChild " << countCld << endl;


   //find out the name,type and value of the children of the LastChild of 
the root
   for (XMLSize_t countChild = 
childNodesOfLastChild->getLength();countChild != 0; countChild--)
   {
     DOMNode* itemNodes =  childNodesOfLastChild->item(countChild - 1);
     const XMLCh* itemString = itemNodes->getNodeName();

     cout << "This is the Name of one Child of LastChild: " << 
XMLString::transcode(itemString);
     cout << endl;
     short nodeType = itemNodes->getNodeType();
     switch (nodeType)
     {
      case(1):
      {
       const XMLCh* nameOfAttribute =_L("id");

       cout << "ELEMENT_NODE" << endl;

       DOMNamedNodeMap* attrNodeMap = itemNodes->getAttributes();

       XMLSize_t countOfNamedItem = attrNodeMap->getLength();

       DOMNode* namedItem=attrNodeMap->getNamedItem(nameOfAttribute);

       const XMLCh* valueOfNamedItem = namedItem->getNodeValue();

       char* pchvalueOfNamedItem=XMLString::transcode(valueOfNamedItem);
       cout << "Node of Type ELEMENT_NODE has the value: " << 
pchvalueOfNamedItem << endl;
       cout << endl;
       break;
       }

      case(2):
      {
        cout << "ATTRIBUTE_NODE" << endl;
        break;
      }

      case(3):
      {
        cout << "TEXT_NODE" << endl;
        const XMLCh* nodeValue = itemNodes->getNodeValue();
        cout <<"Node of Type TEXT_NODE has the value: " << 
XMLString::transcode(nodeValue);
        cout << endl;
        cout << endl;
        break;
      }

      case(4):
      {
        cout << "CDATA_SECTION_NODE " << endl;
        break;
      }

      case(5):
      {
       cout << "ENTITY_REFERENCE_NODE" << endl;
       break;
      }

      case(6):
      {
       cout << "ENTITY_NODE" << endl;
       break;
      }

      case(7):
      {
        cout << "PROCESSING_INSTRUCTION_NODE" << endl;
        break;
      }

      case(8):
      {
        cout << "COMMENT_NODE" << endl;
        break;
      }

      case(9):
      {
        cout << "DOCUMENT_NODE" << endl;
        break;
      }

      case(10):
      {
        cout << "DOCUMENT_TYPE_NODE" << endl;
        break;
      }

      case(11):
      {
        cout << "DOCUMENT_FRAGMENT_NODE" << endl;
        break;
      }

      case(12):
      {
        cout << "NOTATION_NODE" << endl;
        break;
      }
     }
   }
   delete parser;

   XMLPlatformUtils::Terminate();