473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

xerces with C++

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;
}
Jul 20 '05 #1
2 3852


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/
Jul 20 '05 #2
MBR
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/

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bekkali Hicham | last post by:
hi, i have downloaded the latest version 2.4 of Xerces, and unziped it, i end up with a diectory hierarchy like this c:\xerces-2_4_0\XercesImpl.jar c:\xerces-2_4_0\XercesSamples.jar...
1
by: Stu | last post by:
I am trying to build the xerces shared library with 2.3.0 version of their source code on an AIX 5.1 32 bit machine with the following version of the g++ compiler /usr/local/bin/g++ -v Reading...
0
by: Waseem | last post by:
Hi I have looked and tried everything and i still cant sort this out i have no idea why this wont work I am using Xerces Perl on Windows and Debian to try this and it wont work on both of...
0
by: Jim Phelps | last post by:
After having memory leak issues with Xerces-c 2.3.0 for Solaris 2.7 for CC 6.2 I have decided to update to at least 2.4. I have downloaded the binary tarball and have installed it on my...
0
by: Dale Gerdemann | last post by:
I've been trying to use DOM level 3 with xerces-2_6_2. There's a sample called samples/DOM3.java, but I've had trouble with compilation. I've downloaded Xerces-J-bin.2.6.2 and...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
3
by: Matt | last post by:
Hello, Summary: Where can one download a Xerces-C (XML pardser) dynamic library file (.DLL file) for Windows (Win98/WinNT/Win2k/WinXP/Win2003, including server flavors; don't need to support...
2
by: Vlad Zorinov | last post by:
I'm getting the following error after a couple of months of XML processing, using Xerces 2.0.0 in an apache tomcat. Does anyone have any ideas what this problem may be or what I should do to solve...
3
by: Raphael Tagliani | last post by:
(english version below) Bonjour! Je travaille sur un gros projet java, qui parse beaucoup de fichiers xml au lancement d'un serveur. Nous avons un problème de concurrence qu lancement. En...
9
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.