Solved

How can I recursively read in an XML Schema(including user-defined types) with VB?

Posted on 2003-11-24
4
408 Views
Last Modified: 2013-11-19
I'm re-writing a tool of ours that reads in XML Schemas, and generates XML instances of them. The original one was hard-coded to read in only certain schemas, and there are no situations with user-defined schema types like:
<xs:element name="dataPoint1" type="dataPoint" minOccurs="0"/>

The thing is, I need to re-write it to be more general, to take in any schema if possible, so the updated version will need to:

1) Read in the XSD, and save it into a node list(done).
2) Parse through the list, and trigger a flag if the current node is of a user-defined type(done).
3) Recursively go back through the list & replace the given node with the complexType information that defines it.(???)

Still relatively new to VB, and I'm having trouble coming up with the right syntax for recursive functions in VB.

Suggestions anyone? Even some examples for recursion with xml node lists would be helpful.

Thanks!
0
Comment
Question by:Inward_Spiral
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
ashoooo earned 250 total points
ID: 9813303
Try using the MSXML library for vb6

As for the recusive functions, here is an idea how to go about it.

Private Sub LoadXMLIntoTree()
    Dim xmlDoc As MSXML2.DOMDocument
    Dim xmlNode As IXMLDOMNode
    Dim n as Node

    Set xmlDoc = New DOMDocument
   
    xmlDoc.async = False
    xmlDoc.Load "somefile.xml"

    Set xmlNode = xmlDoc

   ' get the root node using the MSXML object

    If Not xmlDoc.childNodes(0).baseName = "xml" Then        ' change this to your own root node
        MsgBox "Error in configuration file"
    End If
   
   ' add the first node to the tree view
   set n = treeview1.Nodes.add (,,GetXMLNodeAttribute(xmlChildNodes(0), "name")

   ' call the recursive function on this node

   LoadNode n, xmlDoc.childNodes(0)
End Sub

Private Sub LoadNode(n as Node, x as IXMLDomNode)
    Dim nChild as Node
    Dim xmlChildNode as IXMLDOMNode

    For Each xmlChildNode In x.childNodes(0).childNodes
        strAttrValue = GetXMLNodeAttribute(xmlProjectNode, "name")
        If Not strAttrValue = vbNullString Then
            Set nChild = treeview1.Nodes.Add (tvwchild, n, strAttrValue) ' create a new child

            ' now call this function on the new node and xml node
            LoadNode nChild, xmlChildNode
        End If
    Next xmlChildNode
End Sub


Public Function GetXMLNodeAttribute(ByVal xmlNode As IXMLDOMNode, ByVal attrName As String) As String
    Dim strAttrValue As String
    Dim xmlAttr As IXMLDOMAttribute
   
    On Error GoTo errHandler
    For Each xmlAttr In xmlNode.Attributes
        If xmlAttr.baseName = attrName Then
            GetXMLNodeAttribute = xmlAttr.Value
            Exit Function
        End If
    Next xmlAttr
   
    GetXMLNodeAttribute = vbNullString
   
errHandler:
    Set xmlAttr = Nothing
    Exit Function
End Function


Hope this helps...
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9813316
I copied and pasted the code from one of my apps.. so please change all instances of xmlProjectNode to xmlChildNode
0
 

Author Comment

by:Inward_Spiral
ID: 9972731
Crap, I got it working and never awarded points.

Sorry, didn't mean to pull a scrooge!
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9972798
Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now