XML to Struct
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=85
</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=85
};
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(=93//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 == =93element2=94 )
{
struct.element2 = atof(strcontent.c_str());
}
XMLSting::release(&content);
XMLString::release(&name);
}
result->release();
resolver->release();
doc->release();
delete parser;
SMLPlatformUtils::Terminate();