XML to Struct

R

rjsteele

Hello,

I am iterating through an XML node with many children. For each
child, I have to store it into an associated stuct. I am looking for a
quicker way to do this. So far, I’ve converted each child’s text
content from string to the correct type defined in the struct. The
struct members are of a variety of types so I think that I have to
write the code to determine what type it has to be converted, as I did
below.

Is there a better way to do this? The way I am going about it, will
result in more than a thousand lines. If I validated against a schema,
would I have to do the conversion before I stored it into the struct?
Can anyone provide a better solution for me? Thanks!

I have an XML document with the following format.

<TRACK>
<PARM>
<element1>1.2345e-163</element1>
<element2>9</element2>
<element3>some text</element4?
etc…
</PARM>
</TRACK>

I need to take the element text content and store it in a struct:

struct a_struct
{
int element1;
double element2;
string element3;
etc…
};

I am parsing the XML with the following:

a_struct struct;
DOMElement* = doc->getDocumentElement();
DOMDOcument* doc = parser->getDocumentElement();
DOMXPathNXResolver* resolver = doc->createNSResolver(element);
DOMXPathResult* result = doc->evaluate(XMLString::transcode(“//TRACK/
PARMS/*), element,
resolver, DOMXpathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL);

XMLSize_t len = result->getSnapshotLength();
DOMNode* node = result->getNodeValue;
char* name = XMLString::transcode(node->getNodeName());
string nodename = name;
DOMText* textnode (static_cast<DOMText*> (node->getFirstChild());

if ( textnode != NULL )
{
char* content = XMLString::transcode(textnode->getData());
string strcontent;
strcontent += content;
//
//I would like to avoid having to do the following for each child.
There are more that 300 child nodes.
//

if ( nodename == “element2” )
{
struct.element2 = atof(strcontent.c_str());
}

XMLSting::release(&content);
XMLString::release(&name);
}

result->release();
resolver->release();
doc->release();
delete parser;
SMLPlatformUtils::Terminate();
 
M

Michael Doubez

Hello,

I am iterating through an XML node with many children.  For each
child, I have to store it into an associated stuct. I am looking for a
quicker way to do this. So far, I’ve converted each child’s text
content from string to the correct type defined in the struct. The
struct members are of a variety of types so I think that I have to
write the code to determine what type it has to be converted, as I did
below.

Is there a better way to do this?

Yes. It is called "XML data binding". It is basically a code generator
from a XML schema but harder to implement when you want to take into
account the variabilities.
There is an open source implementation:
www.codesynthesis.com/products/xsd/

But read carefuly the license, IIRC this force more of less your code
to be GPLed.

You can have more information from the following artima article:
http://www.artima.com/cppsource/xml_data_binding.html

Other engines are available at:
http://www.xmldatabinding.org/
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top