Use Visual Basic to manipulate XML documents China Aviation Information Center Wu Bin
For XML documents, insertion, modification, search and other data operations can be done through an XML parser and a series of objects thereto. Microsoft's XML parser resides in a dynamic link library file called Msxml.dll. Because MSXML is a COM object, you can use it in any compatible ActiveX environment, such as in VB and ASP. This article describes the specific method of manipulating an XML document in Visual Basic.
Load XML document
Create a new VB Standard EXE project. Add a CommandButton control to Form1, set its Name and CAPTION attributes to Cmdload and Load XML. In order to use the MSXML parser, the project item must be set in advance to the reference to MSXml.dll. Select the References command for the Project menu, select Microsoft XML, Version 2.0, or later. Add the following code to the cmdload_click event:
Option expedition
DIM XML AS DomDocument
Private subdload_click ()
SET XML = New DomDocument
Call Xml.Load (app.path & "flight.xml")
'Flight.xml is an XML document describing flight information
End Sub
Run the program, click the Load XML button to load the XML document. The loaded XML document contains a set of node objects arranged in accordance with the tree structure, the top node or the root node is DocumentElement. Each node has a set of properties and methods, each node, can contain one or more sub-nodes, of course, may not contain any nodes. The reference to the root node must first be used to manipulate the XML document. Add the following code after cmdload_click event code:
Dim root as ixmldomelement
Set root = xml.documentelement
Traversing node collection
You can use Visual Basic for Each ... Next loop structure to cycle in the node object collection. Add the following code after cmdload_click event code:
Dim node as ixmldomnode
For Each Node in root.childNodes
Debug.print node.text
NEXT
After running, click the Load XML button, you will see the following output results in the Immediate window:
October 15, 2000
Beijing Capital International Hong Kong Chi-ray China International Airlines Boeing Company
B747 13:00 15:55
Shanghai Pudong Frankfurt Meixi Tashkhe, Eugene Handa Airlines Airbus Industrial Company
A330 21:45 01:45
Note that each node's text attribute contains not only its own attribute value, but also contains the text attribute value of all child nodes. In general, all nodes will not be operated at the same time. You can use the SelectNodes method to get a reference to a specific node subset, and the SelectNodes method returns an ixmldomnodelist object. Add the following code after cmdload_click event code:
Dim flightnodes as ixmldomnodeelist
Set flightnodes = root.selectnodes ("flight")
For Each Node in FlightNodes
Debug.print node.selectsinglenode
("Off-site time") .Text & "-"
& Node.selectsinglenode ("To Station Time") .Text
NEXT
This code uses the SelectNodes method to select all the "flight" nodes under the root node, then use the Selectsinglenode method to select "Off-station Time" and "To Station Time" under the "Flight" node, read and print their text attribute values. You can use the ChildNodes collection to recurrently hierarchically display the contents of the XML document.
First, declare a child process prettyprint in Form1.
Public Sub Prettyprint (Node as Ixmldomnode,
Optional Tablevel as integer = 0)
Dim ChildNode as ixmldomnode
IF node.nodeename <> vbnullstrin
And node.nodename <> "# text" then
Debug.printstring (Tablevel, CHR $ (9))
& "<" & Node.nodename & ""
END IF
If node.haschildnodes dam
For each childnode in node.childnodes
Call prettyprint (ChildNode, Tablevel 1)
NEXT
Else
Debug.printstring (Tablevel 1, CHR $ (9)) & node.text
END IF
If Node.NodeName <> vbnullstring and node.nodeename
<> "# Text" THEN
Debug.printstring
(Tablevel, CHR $ (9)) & ""> "
END IF
End Sub
Then add the second CommandButton control in Form1 to set their Name and CAPTION attributes to cmdprint and print. Add the following code to the event cmdprint_click:
Private subdprint_click ()
If not xml is nothing then
Call prettyprint
(XML.Documentelement)
END IF
End Sub
Run the program. First click on the Load XML button to load flight.xml, then click the Print button. The output results of the hierarchy is clear and easy to read in the Immediate window. The sub-process prettyprint loops in the sub-node of DocumentElement, prints the NodeName property value of each node, and each child node is recursively calling Prettyprint. The inner similarity in each node in the XML document is the most attractive feature of XML, and each child node has the same properties and methods as their parent nodes (although their respective content is usually different), this makes all or in the same document. Some nodes write a certain rule becomes relatively simple.
Modify node content
Like the records in the database, you can also modify the contents of the nodes in the XML document. The following code modifies the text attribute value of the "Publish Date" node in the Flight.xml document:
Set node = root.selectsinglenode ("Release Date")
IF not node is nothing then
Node.Text = "October 20, 2000"
END IF
Once the document content is modified, you can save the modified document using the Save method. The Save method can save the XML document as a file, data stream, or ASP's response object. The usage of the Save method is as follows: Xml.save app.path & "flightupdated.xml"
Add the above two codes in the CMDLOAD_CLICK event, run the program. Click the Load XML button to add a new file flightupdated.xml in the program working directory. Open this file in IE5, you can see the text of the "release date" tag has been changed to "October 20, 2000".
In practical applications, XML can be used as a data transmission format, searchable database, customizable data storage methods, and data sources for display information (requires XSL or programming languages such as VB). Because MSXML is a COM object, you can use it in the ASP page, which is easily manipulated using VBScript or JScript programming.