[xsd-users] dealing with xml written/read on-the-fly

Boris Kolpackov boris at codesynthesis.com
Wed Oct 14 10:38:31 EDT 2009


Hi Cerion,

Cerion Armour-Brown <cerion at kestrel.ws> writes:

> I'll take a proper look as soon as I can, but this does look interesting...
> Not quite clear on one point tho: I see the current example reads in  
> chunks and holds in memory the latest chunk... but I'd need to build up  
> a complete model, reading in chunk by chunk. Not sure if this is a  
> simple step to take.

Yes, that's quite easy. Here is a modified fragment from the example
that constructs the entire object model:

    xml_schema::dom::auto_ptr<DOMDocument> doc (p.start (ifs, argv[1], true));

    // Find the id attribute.
    //
    DOMAttr* id_attr (
      doc->getDocumentElement ()->getAttributeNode (
        xml::string ("id").c_str ()));

    // Use the type and traits aliases from the object model.
    //
    object::id_type id (object::id_traits::create (*id_attr, 0, 0));

    // The next chunk we get is the header element.
    //
    doc = p.next ();
    header hdr (*doc->getDocumentElement ());

    object obj (hdr, id);

    // The rest is position elements.
    //
    for (doc = p.next (); doc.get () != 0; doc = p.next ())
    {
      // Dynamically allocate position instances so that obj can assume
      // their ownership without copying.
      //
      auto_ptr<position> p (new position (*doc->getDocumentElement (), 0, &obj));
      obj.position ().push_back (p);
    }


> And if that is possible, I guess I'd then poll the input file for  
> changes, and call parser->next() if there's anything new...

That's not how it works, actually. You see, the next() call can only 
return at certain points in the document structure. For example, it 
cannot return after half of an element's name has been parsed. In
this case, it will keep calling read() on the stream (and blocking
if there is no data) until it reaches a point in the document structure 
where it can return a complete DOM chunk.

I guess if the producer of your XML guarantees that the data will be 
written one first level element at a time, then this will work as 
expected. If that's not the case then you will need to make another 
plan (e.g., run the parser in a separate thread).

Boris



More information about the xsd-users mailing list