Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

773 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