Solved

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

Posted on 2003-11-24
4
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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