| Chris Finlayson 2003-12-31, 1:28 am |
| Hello:
I'm trying to get typed data from a DOM that has a schema. This seems
trivial and I'd like to know if I'm the right track:
Assume you have the following:
The schema:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="book" type="bookdata"/>
<xsd:complexType name="bookdata">
<xsd:sequence>
<xsd:element name="publish_date" type="xsd:date"/>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float"/>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
The DOM:
<?xml version="1.0"?>
<catalog>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="nn.xsd"
id="bk101">
<publish_date>2000-10-01</publish_date>
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<description>An in-depth look at creating applications with
XML.</description>
</book>
</catalog>
Now, I'd like to get thevalues of the DOM as typed data. Is the
following the right track:
//assume we've set up pXMLDoc....
MSXML2::IXMLDOMNodePtr pNode =
pXMLDoc->documentElement->childNodes->item[0];
MSXML2::IXMLDOMNodePtr pChild = pNode->firstChild;
//just walking through a few children for fun
pChild = pChild->GetnextSibling();
pChild = pChild->GetnextSibling();
MSXML2::IXMLDOMSchemaCollection2Ptr pSchemaCollection;
//get back the schema collection associated with xmldoc
pSchemaCollection = pXMLDoc->namespaces;
_variant_t val = pChild->GetnodeValue();
ISchemaItemPtr pDecl = pSchemaCollection->getDeclaration(pChild);
SOMITEMTYPE oType = pDecl->itemType;
if(pDecl->itemType == SOMITEM_ELEMENT)
{
//recast ISchemaItemPtr to ISchemaElementPtr
ISchemaElementPtr pElement = pDecl;
SOMITEMTYPE oType2 = pElement->Gettype()->itemType;
_bstr_t ty = pElement->Gettype()->name;
//now here do I have to say something like:
//this doesn't seem right.....
if (ty == "string")
val.ChangeType(VT_BSTR);
if (ty == "int")
val.ChangeType(blah blah blah....)
}
Thanks! --Chris
|