Parsing XML with Xerces-C++

Discussion in 'XML' started by Matthias Braun, Oct 14, 2006.

  1. Hilfe!

    Ich habe mir eine Xerces C++ Routine geschrieben, die alle Dokumente des
    XML-Trees parst. Die folgende rekursive Funktion funktioniert allerdings
    nicht 100%. Wer kann helfen?

    Danke, Matthias

    --------------

    void process_nodelist(DOMNodeList * CurrentDOMNodeList)
    {
    int nodeCount = CurrentDOMNodeList->getLength();
    cout << endl << "Start processing nodelist with " << nodeCount << "
    Elements." << endl; // real number = (nodeCount/2-1): "
    for(int i=0; i<nodeCount; ++i)
    {
    DOMNode* currentNode = CurrentDOMNodeList->item(i);
    if(currentNode->getNodeType() && currentNode->getNodeType() ==
    DOMNode::ELEMENT_NODE ) // (nodeCount/2-1) loops
    {
    char *strNodeName =
    XMLString::transcode(currentNode->getNodeName());
    cout << " Name of the node is \"" << strNodeName <<
    "\"" << endl;
    DOMNamedNodeMap* NodeMap = currentNode->getAttributes();
    for (XMLSize_t i=0; i<NodeMap->getLength(); ++i)
    // Loop for the attributes
    {
    cout << "DEBUG: " << NodeMap->getLength() << endl;
    DOMNode* currentNamedNode = NodeMap->item(i);
    char *strNodeMapName =
    XMLString::transcode(currentNamedNode->getNodeName());
    char *strNodeMapValue =
    XMLString::transcode(currentNamedNode->getNodeValue());
    cout << " " << i<< ". NodeMap with name \"" <<
    strNodeMapName << "\" has the value \"" << strNodeMapValue << "\"" << endl;
    }
    DOMNodeList * Nodelist = currentNode->getChildNodes();
    if (Nodelist->getLength() > 1)
    process_nodelist(Nodelist);
    else
    {
    if ((NodeMap->getLength()==0))
    {
    char *str_value =
    XMLString::transcode(currentNode->getTextContent());
    cout << "\t\tValue of node \"" << strNodeName <<
    "\" is \"" << str_value << "\"" << endl;
    }
    }
    }
    }
    }
     
    Matthias Braun, Oct 14, 2006
    #1
    1. Advertising

  2. Matthias Braun

    spiff Guest

    Hi Matthias!

    Englisch wäre besser in einer Gruppe wie dieser. In Deutsch werden es
    nicht ganz so viele verstehen ;-)

    Ich nehme an es werden nicht alle Elemente angesprungen. Ist der Fehler
    nicht einfach in dieser Zeile:

    DOMNodeList * Nodelist = currentNode->getChildNodes();
    if (Nodelist->getLength() > 1)

    Wobei ich annehme, ohne DOMNodeList zu kennen, dass getLength() > 0
    hier stehen müsste ansonsten werden einfach alle nodes nicht
    durchsucht, die nur ein subelement haben.

    Grüße spiff
    http://www.spycomponents.com


    Matthias Braun wrote:
    > Hilfe!
    >
    > Ich habe mir eine Xerces C++ Routine geschrieben, die alle Dokumente des
    > XML-Trees parst. Die folgende rekursive Funktion funktioniert allerdings
    > nicht 100%. Wer kann helfen?
    >
    > Danke, Matthias
    >
    > --------------
    >
    > void process_nodelist(DOMNodeList * CurrentDOMNodeList)
    > {
    > int nodeCount = CurrentDOMNodeList->getLength();
    > cout << endl << "Start processing nodelist with " << nodeCount << "
    > Elements." << endl; // real number = (nodeCount/2-1): "
    > for(int i=0; i<nodeCount; ++i)
    > {
    > DOMNode* currentNode = CurrentDOMNodeList->item(i);
    > if(currentNode->getNodeType() && currentNode->getNodeType() ==
    > DOMNode::ELEMENT_NODE ) // (nodeCount/2-1) loops
    > {
    > char *strNodeName =
    > XMLString::transcode(currentNode->getNodeName());
    > cout << " Name of the node is \"" << strNodeName <<
    > "\"" << endl;
    > DOMNamedNodeMap* NodeMap = currentNode->getAttributes();
    > for (XMLSize_t i=0; i<NodeMap->getLength(); ++i)
    > // Loop for the attributes
    > {
    > cout << "DEBUG: " << NodeMap->getLength() << endl;
    > DOMNode* currentNamedNode = NodeMap->item(i);
    > char *strNodeMapName =
    > XMLString::transcode(currentNamedNode->getNodeName());
    > char *strNodeMapValue =
    > XMLString::transcode(currentNamedNode->getNodeValue());
    > cout << " " << i<< ". NodeMap with name \"" <<
    > strNodeMapName << "\" has the value \"" << strNodeMapValue << "\"" << endl;
    > }
    > DOMNodeList * Nodelist = currentNode->getChildNodes();
    > if (Nodelist->getLength() > 1)
    > process_nodelist(Nodelist);
    > else
    > {
    > if ((NodeMap->getLength()==0))
    > {
    > char *str_value =
    > XMLString::transcode(currentNode->getTextContent());
    > cout << "\t\tValue of node \"" << strNodeName <<
    > "\" is \"" << str_value << "\"" << endl;
    > }
    > }
    > }
    > }
    > }
     
    spiff, Oct 15, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    3
    Views:
    2,010
    Roedy Green
    Sep 27, 2005
  2. bugbear
    Replies:
    0
    Views:
    1,057
    bugbear
    Aug 28, 2003
  3. Kane

    XML parsing with Xerces

    Kane, May 12, 2004, in forum: XML
    Replies:
    0
    Views:
    482
  4. cvissy
    Replies:
    0
    Views:
    627
    cvissy
    Nov 16, 2004
  5. Cigdem
    Replies:
    2
    Views:
    3,525
    Cigdem Savaseri-Brackmann
    Mar 17, 2005
Loading...

Share This Page