xerces with C++

M

MBR

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;
}
 
M

Martin Honnen

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.
 
M

MBR

Thanks Martin,

it is working.

Matthias


Martin said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top