Using XML DOM to display multiple stories in the TreeView

I nternet Explorer 5.0 cho ta Document Object Model (DOM) ActiveX Goi Allah MSXML.DLL hand ta co the dung trong VB6. Dau tien Allah Microsoft XML, version 2.0 , theo đó là TIEPI Microsoft XML, v2.6moi vain Nhat Lo Microsoft XML, v3.0 . CA DLL ba danh trong nay co đều Sach ta hand co cac References include the Chi Dung IDE menu command Project | References .
When you load an XML file into the DOM, it automatically parse XML data to build a multi-Tree nodes with hierarchical parent, child inside. On which we can display a DOM Tree was in a TreeView to visualize the structure of XML data.
 
In the example below, we load an XML file named Library.xml into DOM. This file has an XML Schema file name LibrarySchema.xml . DOM XML file when loaded, it can be recommended test (validated) XML data is considered in accordance with standards required in the Schema file.
Content of Library.xml as follows, note the reference to line 7 LibrarySchema.xml that DOM will be used to validate data in the XML file:


<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="Library.xsl"?>
<!-- Copyright 2000 Wattle Software http://XMLwriter.net/ -->
library xmlns = "x-schema: LibrarySchema.xml" >
<!-- declare the Schema which defines this document -->
<name>Northmead Local Library</name>
<book hardback="yes" availableforloan="no">
<title>C++ Programming for Beginners</title>
<author>
first-name Claude </ first-name >
<last-name>Schwartz</last-name>
</author>
callno 005,133 / C </ callno >
<online_url>http://library/online_books/005133C.html</online_url>
</book>
<journal series="XML Users Journal">
<title>XML Users Journal August 1999</title>
<date>1999-08-01</date>
callno 005,133 / C </ callno >
</journal>
<video>
<title>Titanic</title>
<director>
<name>James Cameron</name>
</director>
callno 643.11 / T </ callno >
</video>
<book>
<title>The C Programming Language</title>
<author>
<first-name>Brian</first-name>
<last-name>Kernighan</last-name>
</author>
<author>
<first-name>Dennis</first-name>
<last-name>Ritchie</last-name>
</author>
callno 005.133/C2 </ callno >
<online_url>http://library/online_books/005133C2.html</online_url>
</book>
</library>

Content of LibrarySchema.xml as follows:

<?xml version="1.0"?>
schema xmlns = "urn: schemas-microsoft-com: xml-data" xmlns: dt = "urn: schemas-microsoft-com: datatypes" >
<!-- Copyright 2000 Wattle Software http://XMLwriter.net/ This Schema is based on XML-Schema support found in Microsoft Internet Explorer 5. -->
<!-- first we need to declare all elements that will appear only as child elements -->
<ElementType name="first-name" content="textOnly"></ElementType>
<ElementType name="last-name" content="textOnly"></ElementType>
<ElementType name="name" content="textOnly"></ElementType>
<ElementType name="price" content="textOnly" dt:type="fixed.14.4"></ElementType>
<ElementType name="title" content="textOnly" dt:type="string"></ElementType>
<ElementType name="artist" content="textOnly" dt:type="string"></ElementType>
<ElementType name="callno" content="textOnly" dt:type="string"></ElementType>
<ElementType name="date" content="textOnly" dt:type="date"></ElementType>
<ElementType name="online_url" content="textOnly" dt:type="string"></ElementType>
<!-- and all attributes too -->
<AttributeType name="hardback" dt:type="string"></AttributeType>
<AttributeType name="availableforloan" dt:type="string"></AttributeType>
<AttributeType name="series" dt:type="string"></AttributeType>
<!-- Now we can define the more interesting elements (i.e. those that can have children ) -->
<ElementType name="author" content="eltOnly" order="one">
<!-- An author can contain EITHER: a name, or a sequence of first-name then last-name -->
<group order="seq">
<element type="name"></element>
</group>
<group order="seq">
<element type="first-name"></element>
<element type="last-name"></element>
</group>
</ element type >
<ElementType name="director" content="eltOnly" order="one">
<!-- A director can contain EITHER: a name, or a sequence of first-name then last-name -->
<group order="seq">
<element type="name"></element>
</group>
<group order="seq">
<element type="first-name"></element>
<element type="last-name"></element>
</group>
</ element type >
<ElementType name="video" content="eltOnly">
<element type="title"></element>
<element type="director"></element>
element type = "callno" </ element >
</ element type >
<ElementType name="cd" content="eltOnly">
<element type="title"></element>
<element type="artist"></element>
element type = "callno" </ element >
</ element type >
<ElementType name="journal" content="eltOnly">
<!-- declare series as an optional attribute of journal -->
<attribute type="series" required="no"></attribute>
<element type="title"></element>
<element type="date"></element>
element type = "callno" </ element >
</ element type >
<ElementType name="book" content="eltOnly">
<!-- declare hardback as an optional attribute of journal with default value of "no" -->
<attribute type="hardback" default="no"></attribute>
<attribute type="availableforloan" default="yes"></attribute>
<element type="title"></element>
<!-- allow for multiple authors with maxOccurs -->
element type = "author" maxOccurs = "*" </ element >
element type = "callno" </ element >
<!-- allow for ONE optional URL -->
<element type="online_url" minOccurs="0" maxOccurs="1"></element>
</ element type >
<ElementType name="library" content="eltOnly">
<!-- the library name comes first -->
<element type="name"></element>
<!-- followed by a collection of books, videos and cds -->
<group order="many">
element type = "book" maxOccurs = "*" </ element >
element type = "journal" maxOccurs = "*" </ element >
element type = "video" maxOccurs = "*" </ element >
element type = "cd" maxOccurs = "*" </ element >
</group>
</ element type >
</ Schema >

The program is first run when you click the button Load and Display XML in TreeView . Wait a little, Tree of XML will appear in the TreeView.
Simultaneously Content of XML files are loaded into the ListBox lstXMLSource and you'll see it if you click Tab XML Source . Of course you can display any XML file if you leave it in the folder of the program and enter it into the TextBox Filename txtXMLFileName before you click the button Load and Display XML in TreeView . 





     In this program we use IXMLDOMNode Object, Object IXMLDOMElement instead turnto go through all nodes of the XML DOM. Program Sub AddNode to call off the Nodes in the TreeView. Especially AddNode calls itself inside AddNode Sub. This technique is called recursive, which often get used in the structure like a tree branch, when I havemore of a different child. Listing of Sub AddNode as follows:



Private Sub AddNode ( ByRef oElem As IXMLDOMNode, Optional ByRef oTreeNode As Node)
' Add a Node to the TreeView
Dim oNewNode As Node
Dim oNodeList As IXMLDOMNodeList
Dim i As Long
' Create the new node
If oTreeNode Is Nothing Then
' Go through here when creating the top level nodes, i.e. childNodes of root node
Set oNewNode = TreeView.Nodes.Add
Else
Set oNewNode = TreeView.Nodes.Add(oTreeNode, tvwChild)
End If
' Expand TreeView node
oNewNode.Expanded = True
' Prepare the Text for the TreeView Node
If oElem.nodeType = NODE_ELEMENT Then
' Element Node type. Use Node name and Attribute values
oNewNode.Text = BuildNodeLabel(oElem)
ElseIf (oElem.nodeType = NODE_TEXT) Then
' Last Node in the branch. Use Text
oNewNode.Text = oElem.Text
ElseIf (oElem.nodeType = NODE_COMMENT) Then
' Comment Node. Display the comment
oNewNode.Text = "Comment:" & oElem.Text
Else
' Display Nodename as default
oNewNode.Text = oElem.nodeName
End If
' process the childNodes which form a NodeList
Set oNodeList = oElem.childNodes
' Iterate through each childNode
For i = 0 To oNodeList.length - 1
' Recursively call AddNode to add more nodes as children of oNewNode,
' treating AddNode just like another Sub
AddNode oNodeList.Item(i), oNewNode
Next
End Sub 


There are three processors in our breed Nodes djay: NODE_ELEMENT, NODE_TEXTWill NODE_COMMENT. 
Node Element Node has the attribute Will Con. How NodeNode text goes there is more text.

You can download sample programs XMLTreeDOM.zip to trial.

For more of the Properties and Methods of the Classes of MSXML, from the VB6 IDE you press F2 to display the Object Browser . When the Object Browser dialog appears, choose MSXML2 above are from ComboBox display <All Libraries> , and then choose a class, such as IXMLDOMNode from the ListBox on the left, details of the selected class will be displayed in the ListBox to the right as shown in Figure below: 

0 comments:

Post a Comment