[xsd-users] Does polymorphic serializer support streaming?

Danny Yue sheepduke at 163.com
Sun Jun 7 08:44:38 EDT 2015


Hi Boris,

Considering a `tree' with a sequence of element `apple' and `orange'
which derived from `node'.

When building the whole tree and calling `tree_(std::cout, tree, map)',
I've got:
`<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<tree xmlns="http://example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <node color="red" name="name" smell="good" xsi:type="apple"/>
  <node color="yellow?" name="orange" xsi:type="orange"/>
</tree>'
which is exactly what I want.

However, when I use serializer like:
// begin
//
s.start(std::cout);
s.next_open("tree", tree);
s.next("apple", *apple);
s.next("orange", *orange);
s.next_close("tree");
// end

I got:
`<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<tree>
  <apple color="red" name="name" smell="good"/>
  <orange color="yellow?" name="orange"/>
</tree>'

in which `xsi:type' is lost and I don't know how to 
serialize `apple' with `xsi:type'.

Am I doing anything wrongly or....too stupid to use this? :-p


XSD and C++ source file is in attached files. I hope I'm not playing
against the rules of the mailing list. :-)


Thanks a lot.
Danny


From: Boris Kolpackov <boris at codesynthesis.com>
Subject: Re: [xsd-users] Does polymorphic serializer support streaming?
Date: Fri, 5 Jun 2015 19:11:08 +0200

> Hi Danny,
> 
> Danny Yue <sheepduke at 163.com> writes:
> 
>> When I serialize the whole tree using generated `Tree_' function,
>> `xsi:type' can be serialized normally. However, when I use the
>> serializer taken from `streaming' directory, those polymorphism
>> information is lost. All the elements start with their own type name
>> instead of their parent's type name and `xsi:type'. i.e.
>> What I want: <node xsi:type="A" attribute="some">
>> Reality: <A attribute="some">
> 
> You would need to provide more details on what exactly you are
> doing during serialization. Specifically, are the polymorphic
> types your "streaming points", i.e., the fragments which you
> serialize one at a time or are they nested inside some other
> bigger fragments? If it is the former, then you should be able
> to specify the element name yourself. If it is the latter,
> then I don't see how the result should be different from
> using the top-level parsing function (i.e., Tree_() in your
> case).
> 
> Boris
-------------- next part --------------
#include <iostream>

#include "tree.hxx"
#include "trace_serializer.hxx"

using namespace std;


int main()
{
    xml_schema::namespace_infomap map;
    map[""].name="http://example.com";
    map["trace"].name="http://trace.com";

    try
    {
        xercesc::XMLPlatformUtils::Initialize();

        tree tree;
        tree::node_sequence seq;

        apple *apple = new class apple;
        apple->name("name");
        apple->color("red");
        apple->smell("good");

        orange *orange = new class orange;
        orange->name("orange");
        orange->color("yellow?");
        
        seq.push_back(*apple);
        seq.push_back(*orange);
        tree.node(seq);

        tree_(std::cout, tree, map);

        cout << endl << endl;

        seq.clear();
        tree.node(seq);

        simsoc_trace::trace_serializer s;
        s.start(std::cout);
        s.next_open("tree", tree);
        s.next("apple", *apple);
        s.next("orange", *orange);
        s.next_close("tree");

        cout << endl << endl;

        node *node = static_cast<class node*>(apple);

        node_(std::cout, *node, map, "UTF-8",
              xml_schema::flags::no_xml_declaration);

        cout << endl << endl;

        node = static_cast<class node*>(orange);
        node_(std::cout, *node, map, "UTF-8",
              xml_schema::flags::no_xml_declaration);
    }
    catch (const xercesc_3_1::DOMException &e)
    {
        cerr << "ERROR:" << e.code << " "
             << *e.getMessage() << endl;
    }
    catch (const xml_schema::exception &e)
    {
        cerr << e << endl;
    }
}
-------------- next part --------------
<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:element name="node" type="node"/>
  <xsd:element name="apple" type="apple"/>
  <xsd:element name="orange" type="orange"/>

  <xsd:complexType name="node">
    <xsd:attribute name="name" type="xsd:string"/>
    <xsd:attribute name="color" type="xsd:string"/>
  </xsd:complexType>

  <xsd:complexType name="apple">
    <xsd:complexContent>
      <xsd:extension base="node">
        <xsd:attribute name="smell" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  
  <xsd:complexType name="orange">
    <xsd:complexContent>
      <xsd:extension base="node"/>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:complexType name="tree">
    <xsd:sequence>
      <xsd:element ref="node" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="tree" type="tree"/>

</xsd:schema>


More information about the xsd-users mailing list