[xsd-users] Serialization Methods For Polymorphic Types

Erik Sjölund erik.sjolund at gmail.com
Mon Oct 3 17:54:28 EDT 2011


Yes, it should be possible. If we modify

examples/cxx/tree/polymorphism

somewhat, I think we see the result you are expecting.

Replace "root-element-last" with
"--root-element supermen --root-element person"
in the file
examples/cxx/tree/polymorphism/makefile

and then incorporate this into
examples/cxx/tree/polymorphism/driver.cxx

-----------------------
auto_ptr<supermen> sm(supermen_ (argv[1]));
xml_schema::namespace_infomap map;
map[""].schema = "supermen.xsd";
for (supermen::person_const_iterator i (sm->person ().begin ());
     i != sm->person ().end ();
     ++i)
{
person_ (std::cout, *i, map);
}
-----------------------------------

If we now run "./driver supermen.xml" we get this output:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="supermen.xsd">

  <name>John Doe</name>

</person>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<superman can-fly="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="supermen.xsd">

  <name>James "007" Bond</name>

</superman>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<person can-fly="true" wing-span="10"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="supermen.xsd" xsi:type="batman">

  <name>Bruce Wayne</name>

</person>


Only one thing is a bit suprising. The last person is a
<person xsi:type="batman"  ...
and not a
<superman xsi:type="batman"  ...
as it is in the file supermen.xml
but I guess they are two equivalent ways of writing the same thing.

cheers
Erik Sjölund



More information about the xsd-users mailing list