Solved

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

Posted on 2003-11-24
4
409 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

919 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

21 Experts available now in Live!

Get 1:1 Help Now