You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Ramon F Herrera <ra...@patriot.net> on 2011/01/02 21:07:46 UTC

Sample code to traverse minimal XML tree

Greetings and Happy New Year to all,

This is my second attempt to learn and embrace Xerces-C, I hope it will 
be a successful one this time. It is a New Year resolution. ;-)

Some time ago I incorporated Xerces into my applications, but the code I 
wrote seemed a very complex way to achieve simple results. I then 
decided that all that work and frustration was not worth the trouble, 
and switched to Java+XPath. It works great, but some of my XML-related 
apps must be written in C/C++.

To further complicate matters, the hard disk where I had all my Xerces 
code died at the worst possible time.

I would like to start with a fresh look at Xerces again. Could any of 
you nice fellows provide a simple, tutorial-like code to traverse a tree 
like the one attached? All what is needed are those numerical values. I 
am sure I can write some sort of code, but like I said I believe I am 
failing to write it in the best/simplest way.

TIA,

-Ramon



Re: Sample code to traverse minimal XML tree

Posted by Alberto Massari <Al...@progress.com>.
Hi Ramon,
if you already tried XPath, you can use it also with Xerces 3.x

         XercesDOMParser parser;
         parser.parse("c:\\simple-sample.xml");
         DOMDocument *document = parser.getDocument();

         XMLCh xpathStr[100];
         
XMLString::transcode("/line-segments/vertical/distance",xpathStr,99);
         DOMXPathResult* result= document->evaluate(xpathStr, 
document->getDocumentElement(), NULL, 
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL);
         int count = result->getSnapshotLength();
         for(int i=0;i<count;i++) {
             result->snapshotItem(i);    // this select the node for the 
getNodeValue call
             DOMNode* item = result->getNodeValue();
             // do something with the item->getTextContent()
         }
         result->release();

Alberto

On 1/2/2011 9:07 PM, Ramon F Herrera wrote:
> Greetings and Happy New Year to all,
>
> This is my second attempt to learn and embrace Xerces-C, I hope it will
> be a successful one this time. It is a New Year resolution. ;-)
>
> Some time ago I incorporated Xerces into my applications, but the code I
> wrote seemed a very complex way to achieve simple results. I then
> decided that all that work and frustration was not worth the trouble,
> and switched to Java+XPath. It works great, but some of my XML-related
> apps must be written in C/C++.
>
> To further complicate matters, the hard disk where I had all my Xerces
> code died at the worst possible time.
>
> I would like to start with a fresh look at Xerces again. Could any of
> you nice fellows provide a simple, tutorial-like code to traverse a tree
> like the one attached? All what is needed are those numerical values. I
> am sure I can write some sort of code, but like I said I believe I am
> failing to write it in the best/simplest way.
>
> TIA,
>
> -Ramon
>
>