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 "Steele, Raymond" <ra...@lmco.com> on 2011/02/01 17:43:55 UTC

XML to Struct

Hello,

I am iterating through an XML node with a 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();



Raymond




Re: XML to Struct

Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi,

deloptes <de...@yahoo.com> writes:

> The only problem seem to be that the library is based on the xerces 2.8
> version

The C++/Parser mapping that you are trying to use can either use 
Xerces-C++ (both 2 and 3-series) or Expat as the underlying XML 
parser (add '--xml-parser expat' for the latter). XSD/e does not
have any external dependencies.

If you have any further questions/problems with either of these
tools, please send them to the corresponding mailing list[1] (you
can post without subscribing) instead of Xerces-C++ mailing list
since here they are off-topic.


[1] http://www.codesynthesis.com/support/

Boris

-- 
Boris Kolpackov, Code Synthesis        http://codesynthesis.com/~boris/blog
Compiler-based ORM system for C++      http://codesynthesis.com/products/odb
Open-source XML data binding for C++   http://codesynthesis.com/products/xsd
XML data binding for embedded systems  http://codesynthesis.com/products/xsde

Re: XML to Struct

Posted by deloptes <de...@yahoo.com>.
deloptes wrote:

> Boris Kolpackov wrote:
> 
>> 
>> Yes, XML data binding allows you to automate this provided you have
>> a schema describing your XML vocabulary.
>> 
>> You can, for example, try CodeSynthesis XSD[1] which uses Xerces-C++
>> underneath. Or, for a lighter-weight, dependency-free variant, you can
>> try CodeSynthesis XSD/e[2].
>> 
>> Both of these tools can either generate the struct's (actually, classes)
>> for you or you can use them to extract the data into your own structures.
>> 
>> [1] http://www.codesynthesis.com/products/xsd/
>> [2] http://www.codesynthesis.com/products/xsde/
>> 
>> Boris
>> 


The only problem seem to be that the library is based on the xerces 2.8
version

regards

library-driver.o: In function
`xercesc_2_8::DefaultHandler::fatalError(xercesc_2_8::SAXParseException
const&)':
library-driver.cxx
(.text._ZN11xercesc_2_814DefaultHandler10fatalErrorERKNS_17SAXParseExceptionE[xercesc_2_8::DefaultHandler::fatalError(xercesc_2_8::SAXParseException
const&)]+0x35): undefined reference to
`xercesc_2_8::SAXParseException::SAXParseException(xercesc_2_8::SAXParseException
const&)'
library-driver.cxx
(.text._ZN11xercesc_2_814DefaultHandler10fatalErrorERKNS_17SAXParseExceptionE[xercesc_2_8::DefaultHandler::fatalError(xercesc_2_8::SAXParseException
const&)]+0x3a): undefined reference to
`xercesc_2_8::SAXParseException::~SAXParseException()'
library-driver.cxx
(.text._ZN11xercesc_2_814DefaultHandler10fatalErrorERKNS_17SAXParseExceptionE[xercesc_2_8::DefaultHandler::fatalError(xercesc_2_8::SAXParseException
const&)]+0x3f): undefined reference to `typeinfo for
xercesc_2_8::SAXParseException'



Re: [ANN]VTD-XML 2.10

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Please stop spamming the mailing list with these announcements that have
nothing to do with Xerces or any project hosted at the ASF.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

"Jimmy Zhang" <cr...@comcast.net> wrote on 02/25/2011 05:08:02 PM:

> VTD-XML 2.10 ...

[ANN]VTD-XML 2.10

Posted by Jimmy Zhang <cr...@comcast.net>.
VTD-XML 2.10 is now released. It can be downloaded at 
https://sourceforge.net/projects/vtd-xml/files/vtd-xml/ximpleware_2.10/.
This release includes a number of new features and enhancement.

* The core API of VTD-XML has been expanded. Users can now perform 
cut/paste/insert on an empty element.
* This release also adds the support of deeper location cache support for 
parsing and indexing. This feature is useful for application performance 
tuning for
processing various XML documents.
* The java version also added support for processing zip and gzip files. 
Direct processing of httpURL based XML is enhanced.
* Extended Java version now support Iso-8859-10~16 encoding.
* A full featured C++ port is released.
* C version of VTD-XML now make use of thread local storage to achieve 
thread safety for multi-threaded application.
* There are also a number of bugs fixed. Special thanks to Jozef Aerts, John 
Sillers, Chris Tornau and a number of other users for input and suggestions 


Re: XML to Struct

Posted by deloptes <de...@yahoo.com>.
Boris Kolpackov wrote:

> 
> Yes, XML data binding allows you to automate this provided you have
> a schema describing your XML vocabulary.
> 
> You can, for example, try CodeSynthesis XSD[1] which uses Xerces-C++
> underneath. Or, for a lighter-weight, dependency-free variant, you can
> try CodeSynthesis XSD/e[2].
> 
> Both of these tools can either generate the struct's (actually, classes)
> for you or you can use them to extract the data into your own structures.
> 
> [1] http://www.codesynthesis.com/products/xsd/
> [2] http://www.codesynthesis.com/products/xsde/
> 
> Boris
> 

great

I was wondering if it is possible too.
I was also trying to build http://incubator.apache.org/xmlbeanscxx/ recently
but without success. codesynthesis seems to provide something smart and
working.

thanks + regards


Re: XML to Struct

Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Raymond,

Steele, Raymond <ra...@lmco.com> writes:

> I am iterating through an XML node with a 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, XML data binding allows you to automate this provided you have
a schema describing your XML vocabulary.

You can, for example, try CodeSynthesis XSD[1] which uses Xerces-C++
underneath. Or, for a lighter-weight, dependency-free variant, you can
try CodeSynthesis XSD/e[2].

Both of these tools can either generate the struct's (actually, classes)
for you or you can use them to extract the data into your own structures.

[1] http://www.codesynthesis.com/products/xsd/
[2] http://www.codesynthesis.com/products/xsde/

Boris

-- 
Boris Kolpackov, Code Synthesis        http://codesynthesis.com/~boris/blog
Compiler-based ORM system for C++      http://codesynthesis.com/products/odb
Open-source XML data binding for C++   http://codesynthesis.com/products/xsd
XML data binding for embedded systems  http://codesynthesis.com/products/xsde