[xsd-users] Print all information for one xml without knowing all tag names

Boris Kolpackov boris at codesynthesis.com
Wed Jun 10 13:03:51 EDT 2009


Hi Dian,

Dian Du <dian.aa.du at gmail.com> writes:
 
> <contact>
>   <name>John Doe</name>
>   <email>j at doe.com</email>
>   <phone>555 12345</phone>
> </contact>
> 
> 
> auto_ptr<Contact> c = contact ("c.xml");
> cout << c->name () << ", "
>      << c->email () << ", "
>      << c->phone () << endl;
> 
> We have to know the name of each tag before calling one of the
> function named as tag, such as c->name ().

That's correct.


> How do I implement a more genetic way, as  as
> 
> auto_ptr<Contact> c = contact ("c.xml");
> 
> while (not end of tags) {
>    cout << c->one of tay();
>    next tag
> }

The whole point of the C++/Tree object model is that it is not
"generic" but rather vocabulary-specific. That is, you call 
functions corresponding to element/attribute names in your XML
vocabulary to access the data.

If your application needs a generic XML access API then DOM or
SAX are the better ways to do it.

Now, for cases where you mostly need a vocabulary-specific access
but have a small part where a generic API is more suitable, the
C++/Tree mapping allows you to maintain a bi-directional association
between object model nodes and corresponding DOM nodes. This way
you can "switch" between using DOM API and C++/Tree API to access
the same data. For more information on this feature see Section
5.1, "DOM Association" in the C++/Tree Mapping User Manual:

http://www.codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#5.1

Boris




More information about the xsd-users mailing list