xerces with C++

Discussion in 'XML' started by MBR, Nov 28, 2004.

  1. MBR

    MBR Guest

    Help!

    I am using xerces with C++ and want to read the following simple file:

    <?xml version="1.0"?>
    <pets>
    <pet>
    <name>Tilly</name>
    <age>14</age>
    <type>cat</type>
    <color>silver</color>
    <pflege morgens="Essen geben" abends="Schlaflied"/>
    </pet>
    <pet>
    <name>Amanda</name>
    <age>10</age>
    <type>dog</type>
    <color>brown</color>
    <pflege morgens="Trinken geben" abends="Schlachten"/>
    </pet>
    </pets>

    I can read the attributes, but not the value of the elements? What is
    wrong? The method "value = testnode.getNodeValue().transcode();" is not
    working.

    Thanks,

    Matthias

    #include <xercesc/dom/deprecated/DOMParser.hpp>
    #include <xercesc/dom/deprecated/DOM_DOMException.hpp> // Alles
    notwendig einzubinden??????
    #include <xercesc/util/PlatformUtils.hpp>
    #include <xercesc/dom/DOMNode.hpp>
    #include <locale.h>
    #include <iostream>
    using namespace std;

    XERCES_CPP_NAMESPACE_USE

    int main(int argc, char* argv[])
    {

    // setzen des aktuellen Gebietsschemas...
    setlocale(LC_ALL,NULL);
    // ...ausgenommen sind Ziffern
    setlocale(LC_NUMERIC,"C");
    try
    {
    XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& c_roToCatch)
    {
    cerr << "Fehler bei der Initialisierung: " <<
    c_roToCatch.getMessage() << endl;
    exit(1);
    };

    DOMParser oParser;
    try // Exception
    funktioniert nicht, wenn die Datei z.B. nicht existiert!
    {
    oParser.parse("test.xml");
    }
    catch(const XMLException& c_roToCatch)
    {
    cerr << "Fehler beim Parsen: " << c_roToCatch.getMessage() << endl;
    exit(1);
    }
    catch(const DOM_DOMException&)
    {
    cerr << "DOM Fehler beim Parsen" << endl;
    exit(1);
    }
    catch(...)
    {
    cerr << "unerwarteter Fehler beim Parsen. " << endl;
    exit(1);
    }

    DOM_Document oDoc = oParser.getDocument();
    if (oDoc != 0) // ab hier kann mit dem
    Dokument gearbeitet werden
    {
    DOM_Element oRoot = oDoc.getDocumentElement(); // zeigt auf
    pets

    if (oRoot != 0)
    {
    string name = oRoot.getNodeName().transcode();
    string value = "";
    cout << "Name des Root-Elements: " << name << endl;
    DOM_Node oNode = oRoot.getFirstChild();
    while (oNode != 0) //
    Iterator ueber die verschiedenen pet
    {
    if (oNode.getNodeType() == DOM_Node::ELEMENT_NODE)
    {
    DOM_Element oDomEl = (DOM_Element&) oNode;
    name = oDomEl.getNodeName().transcode();
    cout << " Node Name: " << name << endl;

    if(oDomEl.getNodeName().equals(DOMString("pet"))) // Iterator
    {
    DOM_Node testnode = oDomEl.getFirstChild(); //
    DOMNodes sind dann name, age, type
    while (testnode !=0)
    {
    name = testnode.getNodeName().transcode();
    if (testnode.getNodeType() == DOM_Node::ELEMENT_NODE)
    {
    DOM_Element oNodeEl2 = (DOM_Element&) testnode;
    if (!name.compare("pflege"))
    {
    DOMString dom_value =
    oNodeEl2.getAttribute(DOMString("morgens"));
    if (dom_value.length() > 0)
    {
    value = dom_value.transcode();
    cout << " Pflege morgens: " << value;
    }
    dom_value =
    oNodeEl2.getAttribute(DOMString("abends"));
    if (dom_value.length() > 0)
    {
    value = dom_value.transcode();
    cout << ", Pflege abends: " << value;
    }
    }
    else // case nodename <> "Pflege"
    {
    cout << " Node Name: " << name;
    DOMString dom_value = oNodeEl2.getNodeValue();
    value = dom_value.transcode();
    value = testnode.getNodeValue().transcode();
    cout << ", Node Value: " << value;
    }
    cout << endl;
    }
    testnode = testnode.getNextSibling();
    }
    }
    };
    oNode = oNode.getNextSibling();
    };
    };

    };
    cout << "Ende, weiter mit return" << endl;
    // getchar();
    return 0;
    }
     
    MBR, Nov 28, 2004
    #1
    1. Advertising

  2. MBR wrote:


    > I am using xerces with C++ and want to read the following simple file:
    >
    > <?xml version="1.0"?>
    > <pets>
    > <pet>
    > <name>Tilly</name>
    > <age>14</age>
    > <type>cat</type>
    > <color>silver</color>
    > <pflege morgens="Essen geben" abends="Schlaflied"/>
    > </pet>
    > <pet>
    > <name>Amanda</name>
    > <age>10</age>
    > <type>dog</type>
    > <color>brown</color>
    > <pflege morgens="Trinken geben" abends="Schlachten"/>
    > </pet>
    > </pets>
    >


    > DOMString dom_value = oNodeEl2.getNodeValue();
    > value = dom_value.transcode();
    > value = testnode.getNodeValue().transcode();


    You misunderstand the DOM, in the W3C DOM the nodeValue of an element
    node is always null, it is not the (text) content of the element.
    In the case of the above document you would for instance need to read
    colorElement.getFirstChild().getNodeValue()
    to read the text value "brown" )that being pseudo code but you get the
    idea, you need to access the first child node of the element and read
    its nodeValue).

    See the W3C DOM specification here
    http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247
    it lists nodeValue for the different types of nodes.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 28, 2004
    #2
    1. Advertising

  3. MBR

    MBR Guest

    Thanks Martin,

    it is working.

    Matthias


    Martin Honnen wrote:
    >
    > MBR wrote:
    >
    > > I am using xerces with C++ and want to read the following simple file:
    > >
    > > <?xml version="1.0"?>
    > > <pets>
    > > <pet>
    > > <name>Tilly</name>
    > > <age>14</age>
    > > <type>cat</type>
    > > <color>silver</color>
    > > <pflege morgens="Essen geben" abends="Schlaflied"/>
    > > </pet>
    > > <pet>
    > > <name>Amanda</name>
    > > <age>10</age>
    > > <type>dog</type>
    > > <color>brown</color>
    > > <pflege morgens="Trinken geben" abends="Schlachten"/>
    > > </pet>
    > > </pets>
    > >

    >
    > > DOMString dom_value = oNodeEl2.getNodeValue();
    > > value = dom_value.transcode();
    > > value = testnode.getNodeValue().transcode();

    >
    > You misunderstand the DOM, in the W3C DOM the nodeValue of an element
    > node is always null, it is not the (text) content of the element.
    > In the case of the above document you would for instance need to read
    > colorElement.getFirstChild().getNodeValue()
    > to read the text value "brown" )that being pseudo code but you get the
    > idea, you need to access the first child node of the element and read
    > its nodeValue).
    >
    > See the W3C DOM specification here
    > http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247
    > it lists nodeValue for the different types of nodes.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
     
    MBR, Nov 29, 2004
    #3
    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. Stefan Siegl
    Replies:
    2
    Views:
    777
    David Zimmerman
    Jul 17, 2003
  2. Roy Benjamin
    Replies:
    3
    Views:
    563
    Mike Schilling
    Jul 23, 2003
  3. Leo Sauermann
    Replies:
    0
    Views:
    1,152
    Leo Sauermann
    Jul 28, 2003
  4. Alexandre Flament

    xerces in .jar.bz2

    Alexandre Flament, Aug 14, 2003, in forum: Java
    Replies:
    0
    Views:
    480
    Alexandre Flament
    Aug 14, 2003
  5. cvissy
    Replies:
    0
    Views:
    639
    cvissy
    Nov 16, 2004
Loading...

Share This Page