XML DOM Getting Started Guide
BRIAN RandellDevelopmentor 1999 Abstract: This article discusses how to access and operate XML documents by performing XML DOM after XML document is revealed by Microsoft (R) XML Parser. (10 pages) What is the exact definition of DOM? How to use XML DOM? How to load a document? How to retrieve information in the XML document How to move back and forth in the document? What are you doing now? Preface If you are Visual Basic (R) developers, you receive some "Scalable Markup Language (XML)" document format data. At this point you must have to get information in the XML document and combine these data into the Visual Basic solution. You will inevitably write code to analyze the contents of the XML file, which is just a text file. But this is no longer a good way to make a good way, and erased the advantages of XML: the structure of the data. The preferred method of retrieving the information in the XML file is to use the XML analysis program. It is very simple, the XML analysis program is a software, it reads the XML file and makes the data available. As a developer of Visual Basic, you want to use an Analysis program that supports XML Document Object Model (DOM) ". The DOM defines the standard command set that the analyst should be revealed so that you can access HTML and XML document content in the program. Supporting the DOM's XML analysis program Remove the data in the XML document and exposes it through a group of objects to which it can be programmed. In this article, you will learn how to access and operate it (msxml.dll) by performing XML DOM after the XML document is revealed by Microsoft (R) XML Parser. Before reading, you should look at the original XML file so that you can make your life more easily and have a concept. The following code reveals the contents of the file CDS.xml containing the compressed disk item. Each item contains information such as artists, titles and tracks. XML Version = "1.0"?>
tracks>
compactdisc>
tracks>
compactdisc>
compactdiscs>
The second line of the above documentation involves the external DTD, or is called the "Document Type Definition" file. DTD defines the layout and expected content of a particular type of XML document. The XML analysis program can use DTD to determine if the document is valid. DTD is just a way to use it helps analyze the program verification document. There is also an increasingly popular verification document to "XML architecture". Unlike DTD, we use XML definition schema, while DTD uses them "interesting" syntax. The following code shows the contents of CDS.DTD used by cds.xml:
This article is not intended to discuss DTD and "XML architecture" in depth. XML Architecture Language (English) is based on XML data (English) annotation (in accordance with W3C). What is the exact definition of DOM? XML's DOM is an object model that reveals the content of the XML document. W3C's Document Object Model (DOM) Level 1 specification typically defines that DOM should reveal why attributes, methods, and events. Microsoft's DOM execution method fully supports W3C standards, and some of its other features are more convenient to use XML files in the program. How to use XML DOM? Use XML DOM by creating an instance of an XML analysis program. To this end, Microsoft reveals XML DOM through a set of standard COM interfaces in MSXml.dll. Msxml.dll contains class libraries and execution code used when using XML documents. If you are using a script, such as VBScript in Internet Explorer, use the DOM by creating an instance of an analyte object with the CreateObject method.
Set objParser = CreateObject ("Microsoft.xmLDom")
If you are using VBScript from Active Server Page (ASP), you should use Server.createObject.
Set objParser = Server.createObject ("Microsoft.xmldom")
If you are using Visual Basic, you can access the DOM by setting a reference to the MSXML class library (provided in msxml.dll). Steps to use MSXML within Visual Basic 6.0 for: Open the Engineering Reference dialog. Select Microsoft XML, V2.0 from the list of available COM objects. If this item is not found, you must first get the MSXML class library. At this time, you can create an analyte object instance. Dim xdoc as msxml.domdocumentset xdoc = new msxml.domDocument
Where can I find msxml.dll? There are two ways to get the MSXML class library:
The Internet Explorer 5.0- MSXML analysis program can be installed is a complete component. Another way is to download the reproducible Microsoft XML Parser version.
After reference to the class library in the Visual Basic project, you can call the analyst, load your document, and use the data in the document.
Maybe you are puzzling now - what I am using now? If you open the MSXML class, use the Visual Basic 6.0 object browser to check its object model, you will find that the object model is so rich. This article demonstrates how to use the DomDocument class and the IXMLDomnode interface to access the XML document.
Figure 1. MSXML Analysis Program Object Model
How to load a document?
To load an XML document, you must first create an instance of the DomDocument class:
Dim xdoc as msxml.domdocument
Set xdoc = new msxml.domDocument
After obtaining a valid reference, use a load method to open the file. The MSXML analysis program can load an XML document from the local disk, and use the UNC to use the URL to load the document.
To load a document from disk, create the following constructor using the load method:
IF xdoc.Load ("c: / my documents / cds.xml") THEN
'Document loading is successful.
'Do something interesting now.
Else
'Document loading is unsuccessful.
END IF
After using the document, you need to release your object reference to it. The MSXML analysis program does not reveal clearly. The best way is to explicitly set the reference to Nothing.
Set xdoc = Nothing
When the analysis program is required to load files, it is not synchronously by default. Change the Boolean Async property of this document can change this feature. It is important to check the ReadyState property of the document, and ensure that the document is ready before starting to check its content. The value of the readyState property may return is one of the following five:
The status value has not been initialized: the loading has not yet begun. 0 Loading: Loading method is being executed. 1 has been loaded: The load method has been completed. 2 Interaction: There is enough DOM available for read-only check, and the data is only partially analyzed. 3 Complete: Data has been loaded and analyzed and can be used to read / write operations. 4
The MSXML analysis program reveals events that track the status of the load process when loading large documents. These events are also useful when loading documents from the URL on the Internet.
To open a file from the URL, use the full format of the URL to specify the location of the file. You must add an HTTP: // prefix in front of the file location.
Here is an example of loading a file from a URL:
XDoc.async = false
If XDoc.Load ("http://www.develop.com/hp/brianr/cds.xml") The 'document is loaded.
'Do something interesting now.
Else
'Document loading is unsuccessful.
END IF
Set the Async property of the document to false, the analyst does not return the control to your code before the document is fully loaded and can operate. If you set it to True, you must check your readyState property before accessing the document, or use DomDocument's event, inform you the code.
Handle
During the various reasons, document loading is likely to fail. The most common cause may be invalid that the document name transmitted to the load method is invalid. Another reason may be that the XML document itself is invalid.
By default, the MSXML analysis program verifies whether DTD or architecture is specified in your document. If the ValidateonParse property referenced by the DomDocument object is set before calling the load method, the analyst does not verify the document.
Dim xdoc as msxml.domdocument
Set xdoc = new msxml.domDocument
XDoc.validateonParse = false
IF xdoc.Load ("c: / my documents / cds.xml") THEN
'Document loading is successful.
'Do something interesting now.
Else
'Document loading is unsuccessful.
END IF
However, warning you in advance: The authentication feature of the closing the analysis program is not a good way to make an application. Incorrect documents have failed to cause various reasons. At least, it provides users with invalid data.
Regardless of the type of fault, you can request the analyst to provide fault information by accessing the ParseError object. To use the properties of the ParseError object, set the reference to the IXMLDompivalSeerror interface of the document. The IxmlDompivalSeError interface reveals that 7 properties can be used to study the cause of the error.
The following example shows a message box and all error messages given by the ParseError object.
Dim xdoc as msxml.domdocument
Set xdoc = new msxml.domDocument
IF xdoc.Load ("c: / my documents / cds.xml") THEN
'Document loading is successful.
'Do something interesting now.
Else
'Document loading is unsuccessful.
DIM STRRText As String
DIM XPE as msxml.ixmlDompivalSeerror
'Get ParseError objects
Set xpe = xdoc.parseerror
WITH XPE
STRERRTEXT = "Your XML Document Failed to Load" & _
Due the following error. "& vbcrlf & _
"Error #:" &. Errorcode & ":" & xpe.reason & _
"Line #:" & .line & vbcrlf & _
"Line Position:" & .LinePos & VBCRLF & _
"Position in file:" & .filepos & vbcrlf & _
"Source text:" & .srctext & vbcrlf & _ "Document URL:" & .url
End with
Msgbox Strerrtext, Vbexclamation
END IF
Set XPE = Nothing
You can use the ParseError object to expose information, display this information to the user, record it into the error file, or try to correct the error.
Retrieve information in XML documentation
After loading the document, the next step is to retrieve information from the document. Although document objects are important, they can still be found that in most time is using the ixmldomnode interface. Use the ixmldomnode interface to read and write separate node elements. Before doing anything, you must understand the 13 node types that MSXML analysis programs usually support. The following table lists several most commonly met the node type.
DOM Node type instances NODE_ELEMENT
The node type is accessed through the two properties exposed by the ixmldomnode interface. The NodeType property reveals the enumeration of the DomNodeType project (some of which have been included in the table above). In addition, you can also use nodetypeString to retrieve the text string of the node type.
When there is a reference to the document, you can start moving back and forth in the hierarchy of the node. From your document reference, you can access the ChildNodes property that provides all the nodes in the document from the top of the document. The ChildNodes property reveals IXMLDomnodeList, which supports Visual Basic for / Each configuration. Therefore, you can enumerate all a single node for the ChildNodes property. In addition, the ChildNodes property reveals the Level property, which can return the number of existing child nodes.
The document object not only reveals the ChildNodes property, but also reveals all a single node. In this way, the HaschildNodes property of ixmldomnode is combined, so that the elements, attributes, and values are checked back and forth in the node level. It is very simple.
One thing that needs to be reminded is that the primary relationship between document elements and element values. For example, in a CD XML document, the element
How to move back and forth in the document?
In the XML document, you can move back and forth in a node that is revealed by the document object. Because the XML document is hierarchical, the recursive routine written back and forth in the entire document is relatively easy.
The LoadDocument routine opens an XML document. The loadDocument then calls another routine DisplayNode, which is really moving back and forth in the document. LoadDocument will reference the ChildNodes property that is passed as parameters and integer values to the current open XML document, specifies where to start regenerating levels. The code uses a constant parameter to format the display of text in the Visual Basic "Quick Window" of the document structure. The DisplayNode function is moved back and forth in the document and looks for node-specific node_text types. When the code finds the node_text type node, it retrieves the text of the node with the NodeValue property. In addition, the ParentNode property of the current node is used to get the backtrack reference of the Node_Element type node. Node_element type node reveals the NodeName property. The content of NodeName and NodeValue is displayed.
If the HaschildNodes property is checked, it is determined that the node has a child node, then DisplayNode recursives itself until it reaches the end of the document.
The displayNode routine uses Debug.print to write the information to the "Quick Window" of Visual Basic:
Public Sub loadingDocument ()
Dim xdoc as msxml.domdocument
Set xdoc = new msxml.domDocument
XDoc.validateonParse = false
IF xdoc.Load ("c: / my documents / sample.xml") THEN
'Document loading is successful.
'Do something interesting now.
DisplayNode xdoc.childNodes, 0
Else
'Document loading is unsuccessful.
'View the error message listed above.
END IF
End Sub
Public Sub DisplayNode (byref nodes as msxml.ixmldomnodelist, _
Byval Indent As Integer
Dim xnode as msxml.ixmldomnode
Indent = indent 2
For each xnode in nodes
IF xnode.nodetype = node_text then
Debug.print Space $ (Indent) & XNode.ParentNode.NodeName &_
":" & xnode.nodevalue
END IF
IF xnode.haschildnodes then
DisplayNode Xnode.childNodes, Indent
END IF
Next xnode
End Sub
DisplayNode uses the HaschildNodes property to determine if it should call itself. You should also use the LEVEL attribute of the node and check the value greater than 0.
What are you doing now?
This article is only used as a entry. Now you can learn in depth, enhance the understanding of the XML and MSXML analysis programs. You can do a lot of interesting things, such as updating the value of the independent node item, search in the document, create your own document, and so on. If you need more instances, articles, and downloads, visit the MSDN Online XML Developer Center.