SOM or Schema Object Model is the programming interface defined by Microsoft and can be used to walk through the elements of an XML Schema document and obtain information about the properties and declarations, and about the relationships between them. That is, the DOM is to an XML document what the SOM is to an XSD document.
Tip: SOM was first introduced in MSXML 4.0.
- MSXML 4.0 SOM Documentation
- MSXML 4.0 SOM Reference
Tip: Chapter 3 (Schemas in MSXML) in the book XML Application Development with MSXML 4.0 covers MSXML SOM Implementation in great details.
Let's see an example of how to use MSXML SOM. The following VB example prints each node and its data type (by looking at the schema). First let's look at the source XML file and XSD schema file:
XML Document (emp.xml) |
XSD Schema File (emp.xsd) |
<?xml version="1.0"?>
<empNS:employees xmlns:empNS="urn:ISEmployees">
<employee>
<name>Tom Thomas</name>
<salary>3000</salary>
<startdate>2000-08-14</startdate>
</employee>
</empNS:employees>
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:ISEmployees"
xmlns:e="urn:ISEmployees">
<xs:element name="employees" type="e:empDetails"/>
<xs:complexType name="empDetails">
<xs:sequence>
<xs:element name="employee" type="e:empData"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="empData">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="salary" type="xs:float"/>
<xs:element name="startdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
|
Visual BasicStart Visual Basic 6.0, add reference (Project | Reference) to Microsoft XML, v4.0 and write following code in the Form_Load method:
On Error Resume Next
Dim sc As New MSXML2.XMLSchemaCache40
Dim doc As New MSXML2.DOMDocument40
Dim node As MSXML2.IXMLDOMNode
Dim cntChildNodes As Integer
Dim iIndex As Integer
Dim docNamespaces As New MSXML2.XMLSchemaCache40
Dim oDecl As MSXML2.ISchemaElement
sc.Add "urn:ISEmployees", "c:\emp.xsd"
Set doc.schemas = sc
doc.async = False
doc.setProperty "SelectionNamespaces", "xmlns:empNS='urn:ISEmployees'"
doc.Load "c:\emp.xml"
cntChildNodes = doc.documentElement.childNodes.Item(0).childNodes.length
For iIndex = 0 To cntChildNodes - 1
Set node = doc.documentElement.childNodes.Item(0).childNodes.Item(iIndex)
Set docNamespaces = doc.namespaces
Set oDecl = docNamespaces.getDeclaration(node)
If TypeName(oDecl.Type) = "ISchemaType" Then
MsgBox "Data type of " & node.nodeName & " is " & oDecl.Type.Name
End If
Next
MSXML SOM Resources Around the Web
|