[xsd-users] a couple of questions

Boris Kolpackov boris at codesynthesis.com
Fri Aug 18 14:19:06 EDT 2006


Hi Doug,

Chappelle, Douglas (Com US) <douglas.chappelle at siemens.com> writes:

> [1] I have an object factory that returns auto_ptr<type> (type of course
> being the base type of the xsd objects created). Is there an easy way
> (api) to find out what element this is?
>
> For example:
> 	auto_ptr<type> obj;
> 	obj = myXsdObject(xml_data);
> 	obj->? Returns "myXsdObject"

The same type can be used for more than one element (or even attribute)
so there is no information about this stored by the type itself. You
can, however, use the DOM association feature to obtain this
information. There is the keep_dom flag which you can pass to parsing
functions. You can then use the xml_schema::type::_node member
function to obtain a DOM node that corresponds to this tree node.
Cast it to DOMElement and you can get to the element's name. See
the 'mixed' example for more information on the keep_dom feature.


> [2] I'm reading through the Tree User manual looking for an easy way to
> serialize an auto_ptr<type> to an output stream. I see section 4.5
> Serializing to std::ostream but is there a way to do this without
> needing to build a namespace infomap?

I assume here that you want to serialize auto_ptr<type> generically,
without limiting yourself to some predefined set of types.

There is a way to achieve this though t requires some extra work
from your side. You can serialize an instance of a generated type
to a DOM element (or attribute if it is a simple type). For that
you would use one of the operator<< operators that are generated
when you specify the --generate-serialization option. This implies
that you will need to construct the DOMDocument yourself. If we
have a generated type Foo, then the code would look like this (see
the section 4.1, "Serializing to DOM" of the manual for how to
create a DOMDocument instance):

const Foo& foo = ...

DOMDocument* doc = ... // Create DOM document.
DOMElement* root = doc->getDocumentElement ();

root << foo; // Serialize foo to root.

Now to the tricky part of serializing xml_schema::type instead
of Foo. In order to support XML Schema polymorphism (xsi:type
and substitution groups), XSD generates a map of a type id to
serialization operator for each generated type. This only
happens when you specify the --generate-polymorphic option. The
idea is to take advantage of this map in order to find the
serialization function for instance's dynamic type. Here is the
code that accomplishes this:

auto_ptr<xml_schema::type> t = ...
DOMElement* root = ...

{
  using namespace xsd::cxx::tree;

  typedef type_name_map<char> type_map;
  const type_map& map (type_name_map_instance<0, char> ());

  const type_map::type_info* ti (map.find_type (typeid (*t)));

  if (ti == 0)
  {
    // No type information: compiled without --generate-polymorphic?
  }

  ti->serializer () (root, *t);
}

And the final note: in the current version (2.2.0) both
type_name_map::type_info and type_name_map::find_type are private.
You will need to make them public (in libxsd/xsd/cxx/tree/type-name-map.hxx)
in order for the above code to compile. In the next version of XSD they
will be public.


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060818/2b1dfed8/attachment.pgp


More information about the xsd-users mailing list