Hi Vijay,

Vijay S. Bajwa <[EMAIL PROTECTED]> writes:

> In the past week or so, I have been soaking up a lot on XML and DOM parsing 
> and
> the like. The problem is a familiar one: Given an XML instance doc, parse it,
> while validating it against a schema, and get the stuff into a C/C++ struct.
> Simple enough? Maybe not quite. Here's what I want to do:
>
> 2. Use the XercesDOMParser to walk thru all nodes, and dump the values into my
> c-struct. I imagine I would define some sort of a handler to the
> XercesDOMParser, which would be called as it traverses nodes, making data and
> other info (type, attrs etc) available to me. Which handler would that be?

DOM represents an XML instance as a tree-like in-memory data structure.
There are no events. If you want an even-driven approach, I suggest that
you consider SAX. Also note that both DOM and SAX represent XML in terms
of attributes, elements, and text. There are no types[1] or parsed values
such as integers, dates, etc. -- everything is text.


> 3. If the parser is using the schema, how will the handler make the data
> available to me, if it passed validation. For eg. if I have 2 nodes, an
> xs:integer and another one xs:string in the schema, then can I just say
> something like this?
>
>     if ( strcmp(node.getName(), "quantity") == 0) {
>       int qty = (int)node.getData() ;
>     } else if ( strcmp(node.getName(), "name") == 0) {
>       char* name = node.getData() ;
>     }

No, you will have to parse text representations to the corresponding data
type yourself.


> Given the bewildering number of ways you can do this, is this the best
> approach?

You may want to consider using an XML data binding tool. Such a tool
will compile you XML Schema and generate C++ classes that correspond
to your schema types as well as parsing and serialization code. This
way you can access you data directly without dealing with elements,
attributes, or text. For example, consider this XML Schema type:

<complexType name="Product">
  <sequence>
    <element name="name" type="string"/>
    <element name="quantity" type="integer"/>
  </sequence>
</complexType>


The generated C++ class would then look something like this:

class Product
{
public:
  std::string name () const;
  int quantity () const;
};


You can then access the information directly:

Product p = product ("product.xml");

if (p.quantity () > 0)
{
  cout << p.name () << endl;
}


One of such XML Schema to C++ compilers is CodeSynthesis XSD. It is open-
source and available on a wide range platforms. It also provide an event-
driven mapping which allows you to register hooks instead of getting an
in-memory representation:


http://www.codesynthesis.com/products/xsd/



[1] You can request the parser to maintain association between
    elements/attributes and their schema declarations. This way,
    for example, you can eventually get hold of an XML Schema type
    name (provided the type is not anonymous) for an element or
    attribute.

hth,
-boris

-- 
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to