Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xerces with C++

Reply
Thread Tools

xerces with C++

 
 
MBR
Guest
Posts: n/a
 
      11-28-2004
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;
}
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-28-2004


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-Cor...#ID-1950641247
it lists nodeValue for the different types of nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
MBR
Guest
Posts: n/a
 
      11-29-2004
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-Cor...#ID-1950641247
> it lists nodeValue for the different types of nodes.
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Upgrade of Xalan 1.2.2 and Xerces 1.4.4 to Xalan 2.6 and Xerces 2.6.2 cvissy XML 0 11-16-2004 07:06 AM
xerces in .jar.bz2 Alexandre Flament Java 0 08-14-2003 04:06 PM
Solution for MissingResourceException in apache standard taglib with xerces Leo Sauermann Java 0 07-28-2003 12:29 PM
Sun ONE, The xerces problem (Sun One Oh my) Roy Benjamin Java 3 07-23-2003 08:32 PM
[dom4j & Xerces] How to show validation errors? Stefan Siegl Java 2 07-17-2003 12:26 PM



Advertisments