XML to TreeView

webmasterbeta

New Member
I thought I posted this Friday but I do not see it so I'm reposting it.Does anyone have a sample subroutine that converts XML into a tree view?I'm making a little testing container that I'm able to modify and view XMLin and I'm trying to create a SIMPLE XML to TreeView sub.I can make it work with a lot of code but I think there is a simple way todo it but I'm kinda stuck on my current design and can not see past the obvious.I think someone else might be able to quickly see what's going on.I'm pasting the VB code at the bottom.I found a simple way to handle the XML to TreeView but the code is only processingthe last ChildNode and not each childnode. The only way I can get it toprocess them all is to create nested IF statements. That's not what I wantbecause I want it to work regardless of the number of nested nodes in theXML. If I write the code with nested IF's then I will have to have one pernested node level... which defeats what I'm trying to do.The first thing I see that is wrong with my code is that I loop through eachNode in XMLNode.childNodes inside the sub. I don't think I should do this.I think taking it out and only having the code sub add the current element/nodewould be best. The rest of the reading of the XML would be done in the parentSub. I didn't look at using the nextsibiling and child methods so that maybe the answer.I hope this make sense! Any ideas?Here is the code that works best... but keep in mind it would have to bemodified for each unique XML document you want to process. If you want torun it just create a VB form with a button, MSTreeView, 6 labels and 2 textboxes. The lables and textboxes are just used to display the propertiesof each of the nodes you select on.Dim xmlDoc As MSXML.DOMDocumentDim xmlRootNode As MSXML.IXMLDOMNodeDim Node As MSXML.IXMLDOMNodePrivate Sub Command1_Click()Dim strRelative As StringDim strRelationship As StringDim strKey As StringDim strParentKey As StringDim strText As StringDim intCountChildNodes As IntegerSet xmlDoc = New MSXML.DOMDocumentxmlDoc.loadXML ("<IFX><InsuranceRq><RqUID>12345</RqUID><ClaimSummaryRq><UserValidation><UserName/><Password/></UserValidation><InputCriteria><PolicyNumber/><StartDt/><NumberOfDays/></InputCriteria></ClaimSummaryRq></InsuranceRq></IFX>")Set xmlRootNode = xmlDoc.childNodes.Item(0)strRelationship = tvwChildstrKey = xmlRootNode.baseNamestrText = xmlRootNode.baseNameTreeView1.Nodes.Add , strRelationship, strKey, strTextRtCode = AddNode(strKey)RtCode = AddNode(RtCode)RtCode = AddNode(RtCode)RtCode = AddNode(RtCode)End SubPrivate Function AddNode(ByVal strKey As String) As StringDim strRelative As StringDim strRelationship As StringDim strText As StringstrParentKey = strKeySet xmlNode = xmlDoc.selectSingleNode(strParentKey)For Each Node In xmlNode.childNodesstrRelative = strParentKeystrRelationship = tvwChildstrKey = strRelative & "/" & Node.baseNamestrText = Node.baseNameTreeView1.Nodes.Add strRelative, strRelationship, strKey, strTextNextAddNode = strKeyEnd FunctionPrivate Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)Text1.Text = Node.TextLabel4.Caption = Node.childrenLabel6.Caption = Node.FullPathEnd Sub
 
Back
Top