XML DOM Beginner's Guide Author: Dongsheng Tao Overview: This paper discusses how to access and maintain XMLDOM XML document, the parser XMLDOM implemented by Microsoft. Directory: Introduction DOM is how to use DOM how to process an document How to get information from the XML document How to traverse the XML document Next to do this: As a VB developer you may contact the Extensible Markup Language (XML) document. You now want to handle the XML document and integrate it into your solution. You can program the resolution, handle it as a normal text document, but so too efficient, no power of XML: it can be structured. The best way to get information from the XML file is to use the XML parser. The parser, simple, is a software that can make the data in the XML file easy to use. As a developer of VB, you may want to get a parser that supports Document Object Model (DOM). The DOM describes a range of methods for accessing XML and HTML documents, which should be implemented. A parser that supports DOM should turn data in XML into a series of objects so that these objects can be programmed. In this article, you will learn how to use the DOM structure implemented by the Microsoft parser (MSXml.dll) to access and maintain an XML document. When we continue to advance, let's take a look at a paragraph XML code to understand how this parser makes our life easier. Next, this document called CDS.xml is used to represent each item of a record, and each item contains information such as main singing, title, and track.
XML Version = "1.0"?>
This article does not discuss DTD and XML Schemas, and XML-DATA-based XML Schema Reference has been submitted to W3C. What DOM is: XML DOM structure implements the content of the XML document as an object model. W3C Dom Level 1 illustrates how DOM structures implement properties, methods, events, and more. Microsoft's DOM implementation fully supports W3C standards, and there are many new features that make programs easier to access XML files. How to use DOM Use DOM, you need to create an instance of an XML parser. Microsoft created a series of standard COM interfaces in MSXml.dll to make the creation instance possible. Msxml.dll contains type libraries and applicable code, which you can use to process XML files. If you use clients that implement scripts, such as VBScript and IE, you can use the CreateObject method to get an instance of the parser. Set objParser = CreateObject ("Microsoft.xmldom") If you use the ASP (Active Server Page), you use the Server.createObject method. Set objParser = Server.createObject ("Microsoft.xmLDom") If you are using VB, you can create a reference to the MSXML type library so you can access the DOM. To use MSXML in VB6.0, the operation is as follows: Open the Project References item Select Microsoft XML from the COM object, Version 2.0, if you can't find this, you need to get it. You can create an instance of a parser. Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument You can get MSXml.dll through two ways.
You can install IE5.0, and the MSXML parser is in which components are integrated. Or you can download it on related websites Once you have established a reference to the type library, you can perform resolution, transfer to document, in short, you can handle the XML document. You may have some confusion, what should I do? If you open the MSXML library, use the Visual Basic 6.0 object to view the object model in the viewer, you will find it very rich. This article will tell you how to use the DomDocument class and the IxmLDomnode interface to access the XML document. How to load a document: To transfer an XML document, you must first create an instance of DomDocument. Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument When you get a legal reference, you can use the LOAD method to transfer a document. The parser can be transferred from the local hard drive or transferred from the network through UNC and URL. From the hard disk to the following: if xdoc.load ("c: / my documents / cds.xml") THEN 'Document Tune Success' Do something we like Else' document transferred failures End if you finish your work, you Need to release this reference, MSXML does not directly implement a Close method, you'd better set it directly to Nothing to close it. Set xdoc = Nothing When you call a document, the default is asynchronous, you can change it by modifying the Async property. If you want to operate a document, you must first check the ReadyState property to confirm the status of the document, which will return five possible results. The status attribute value is not initialized: Turn into the document does not start 0 to transfer: LOAD method is executing 1 Transfer completion: The LOAD method has completed 2 interaction phase: DOM can perform a read-only test, data part resolution 3 complete: Data is fully resolved, Read / write operation. 4
The MSXML parser implements some useful methods where you can use these methods to track the status of the transfer process when you transfer a large document. These methods are also very helpful to transfer from the Internet. To open a document on an Internet, you need to provide an absolute URL and must add an HTTP: // prefix. Below is an example. XDoc.async = false if xdoc.load ("http://www.develop.com/hp/brianr/cds.xml") The 'Document Tune Success' Do what we like ELSE' document transfer failback If set the Async property to false so that the parser will not give the control to your code before the document is completed. If you save Async as True, you must check the readyState property when you access the document or use DomDocument's event to prompt your code when you can access the document. Processing error: Your document may be transferred in a variety of reasons, the most common reason is that the document name provided to the parser is incorrect, and another common reason is that the XML document is not legal. The default parser will verify that your document meets a DTD or Schema, you can do not allow the parser to perform verification, before executing the Load method, the DomDocument's properties ValidateonParse is false. Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument xdoc.validateonparse = false if xdoc.load ("c: / my documents / cds.xml") Then 'document transfer success' as we like things ELSE' Document Tune failure END IF is not a good idea in advance, it will bring a lot of problems, at least it will be the user you provide for you. You can get information about the error type from the parser by accessing the ParseError object. Create a reference to the IXMLDompivalSeError interface and point it to the document itself ParseError object. The IxmlDompivalSeError interface implements seven properties to make you get the wrong reason. The following example shows a message box listing all error messages in the ParseError object.
Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument if xdoc.load ("c: / my documents / cds.xml") The 'document is successful "Do what we like ELSE' document transfer DIM DIM STRERRTEXT AS STRING DIM XPE AS MSXML.IXMLDompivalError 'Get ParseError object set xpe = xdoc.parseerror with xpe strerrtext = "Your XML Document cannot be transferred to" & VBCRLF & _ "error #:" Errorcode & ":" & xpe.reason & _ "line #:" & .line & vbcrf & _ "line position:" & .linepos & vbcrlf & _ "Position in file:" & .filepos & vbcrf & _ "Source Text: "& .SrcText & Vbrlf & _" Document URL: "& .url End with Msgbox Strerrtext, vbexclamation end if set xpe = Nothing You can use the ParseError object to report an error message to your user, or write it to you In a log, you can try yourself to solve the problem. How to get information from the XML document: Once you have successfully transferred the document, the next step is how to get information. When you operate a document, you often use the Ixmldomnode interface, you use it to read / write each separated node element. Before using it, you must first understand the 13 types of node elements supported by MSXML, and below is the most commonly used. DOM Node type example NODE_ELEMENT
You access the type of node through the two properties implemented by the IXmldomnode interface. The NodeType property lists all items of DomnodeType (some items are listed on the table above). In addition, you can use the NodeTypeString property to get a string of the node type. Once you have a DOM reference for the document, you can traverse the hierarchy of the node. With documentation, you can access the ChildNodes property that gives a directory that contains all nodes from top. The ChildNodes property implements ixmldomnodelist, which supports Visual Basic's For / Each structure, so you can list all nodes in ChildNodes. In addition, the ChildNodes property also implements the Level property, which can return all child nodes. Not just a document object has a ChildNodes property, each node has a ChildNodes property. Because of this, the ChildNodes property and the IXmLDomnode's HaschildNodes property match make it very convenient to traverse documents, access elements, properties, and values. It is worth mentioning that the element and element values are parent and child relationships. For example, in the CDS XML document, the element