[xsd-users] How to create an object from the xml data without knowing the element type?

Gerard Lanois gerardlanois at gmail.com
Sun Aug 20 16:54:53 EDT 2006


On 8/16/06, Boris Kolpackov <boris at codesynthesis.com> wrote:
> using namespace xercesc;
>
> DOMDocument* dom = ... // Parse XML into DOM.
> DOMElement* root = dom->getDocumentElement ();
> std::string name (xsd::cxx::xml::transcode (root->getLocalName ()));
>
> if (name == "foo")
> {
>   std::auto_ptr<Foo> f (foo (*dom)); // Parse dom to Foo.
>
>   // Do something useful with f.
> }
> else if (name == "bar")
> {
>   std::auto_ptr<Bar> b (bar (*dom)); // Parse dom to Bar.
>
>   // Do something useful with b.
> }
>
>
> If you do not worry about performance much, you can just try calling
> each parsing function in a sequence; all except one will fail:
>
> while (true)
> {
>   try
>   {
>     std::auto_ptr<Foo> f (foo (*dom)); // Try to parse dom to Foo.
>
>     // Do something useful with f.
>     break;
>   }
>   catch (xml_schema::unexpected_element const&)
>   {
>     // Try the next function.
>   }
>
>   try
>   {
>     std::auto_ptr<Bar> b (bar (*dom)); // Try to parse dom to Bar.
>
>     // Do something useful with b.
>     break;
>   }
>   catch (xml_schema::unexpected_element const&)
>   {
>     // Try the next function.
>   }
> }

I think this example would be a very useful addition to the
documentation (maybe in in a "beginners" or "getting started"
section).  And perhaps you could expand on the comment above which
states "// Parse XML into DOM."

-Gerard



More information about the xsd-users mailing list