|
-
Apr 15th, 2020, 07:58 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Populating XML DOM content to List Box
Hi there,
I have an XML file and structured like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<applications>
<app id="b461ae4a" valid="1" company="Orange Inc." appname="None" user="tommy" description="Test" note="None" />
<app id="1c94395b" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
<app id="d65k274p" valid="0" company="Lemon Inc." appname="None" user="admin" description="Test" note="None" />
</applications>

And I wish to be able to populate / enumerate field by filed to List Box, but I'm missing something out and my loop is getting errors:
Code:
Dim xmlDoc As Object
Dim xmlNode As Object
Dim xmlNodes As Object
Dim stXmlFile as String
Dim itCnt As Integer
stXmlFile = "C:\apps.xml"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = "False"
If Not xmlDoc.Load(stXmlFile) Then
Set xmlNodes = Nothing
Set xmlDoc = Nothing
Debug.Print "Error"
Exit Sub
End If
For itCnt = 0 To xmlDoc.selectNodes("//applications").length
ListBox.AddItem xmlDoc.selectNodes("//applications//app").Item(itCnt).selectSingleNode("id").Text
ListBox.AddItem xmlDoc.selectNodes("//applications//app").Item(itCnt).selectSingleNode("valid").Text
ListBox.AddItem xmlDoc.selectNodes("//applications//app").Item(itCnt).selectSingleNode("company").Text
' ... etc...
Next itCnt
also tried like this:
Code:
Set xmlNodes = xmlDoc.SelectNodes("//applications//app")
For Each xmlNode In xmlNodes
ListBox.AddItem xmlNodes.getAttribute("id")
ListBox.AddItem xmlNodes.getAttribute("valid")
ListBox.AddItem xmlNodes.getAttribute("company")
Next
But without any luck.
Any help would be highly appreciated!
Kind regards,
Viktor
Last edited by beic; Apr 15th, 2020 at 09:05 AM.
Reason: typo
-
Apr 15th, 2020, 09:52 AM
#2
Re: Populating XML DOM content to List Box
try like this
Code:
Private Sub Command1_Click()
Dim xmlDoc As MSXML2.DOMDocument60
Dim currentRootElement As MSXML2.IXMLDOMElement
Dim currentNodeElement As MSXML2.IXMLDOMNode '
Dim Temp As MSXML2.IXMLDOMNamedNodeMap
Dim temp2 As MSXML2.IXMLDOMElement
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.Load ("E:\beic.xml")
Set currentRootElement = xmlDoc.documentElement
'Traverse through all child
For Each currentNodeElement In currentRootElement.childNodes
Set Temp = currentNodeElement.Attributes
If Temp.length > 0 Then
Set temp2 = currentNodeElement
Debug.Print "Mother Element Name = " & currentRootElement.nodeName & vbCrLf & "Child Element Name = " & currentNodeElement.nodeName & vbCrLf & "Company = " & temp2.getAttribute("company")
Debug.Print "----------------------------------"
End If
Next
Set xmlDoc = Nothing
End Sub
and the Output
Code:
Mother Element Name = applications
Child Element Name = app
Company = Orange Inc.
----------------------------------
Mother Element Name = applications
Child Element Name = app
Company = Lemon Inc.
----------------------------------
Mother Element Name = applications
Child Element Name = app
Company = Lemon Inc.
----------------------------------
hth
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 15th, 2020, 01:17 PM
#3
Thread Starter
Addicted Member
Re: Populating XML DOM content to List Box
 Originally Posted by ChrisE
try like this
Code:
Private Sub Command1_Click()
Dim xmlDoc As MSXML2.DOMDocument60
Dim currentRootElement As MSXML2.IXMLDOMElement
Dim currentNodeElement As MSXML2.IXMLDOMNode '
Dim Temp As MSXML2.IXMLDOMNamedNodeMap
Dim temp2 As MSXML2.IXMLDOMElement
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.Load ("E:\beic.xml")
Set currentRootElement = xmlDoc.documentElement
'Traverse through all child
For Each currentNodeElement In currentRootElement.childNodes
Set Temp = currentNodeElement.Attributes
If Temp.length > 0 Then
Set temp2 = currentNodeElement
Debug.Print "Mother Element Name = " & currentRootElement.nodeName & vbCrLf & "Child Element Name = " & currentNodeElement.nodeName & vbCrLf & "Company = " & temp2.getAttribute("company")
Debug.Print "----------------------------------"
End If
Next
Set xmlDoc = Nothing
End Sub
and the Output
Code:
Mother Element Name = applications
Child Element Name = app
Company = Orange Inc.
----------------------------------
Mother Element Name = applications
Child Element Name = app
Company = Lemon Inc.
----------------------------------
Mother Element Name = applications
Child Element Name = app
Company = Lemon Inc.
----------------------------------
hth
Hey ChrisE,
I could adopt and your code it's working great, but after a while I found out that I misspelled the Listbox name, it should look like this:
Code:
Set xmlNodes = xmlDoc.SelectNodes("//applications//app")
For Each xmlNode In xmlNodes
List1.AddItem xmlNodes.getAttribute("id")
List1.AddItem xmlNodes.getAttribute("valid")
List1.AddItem xmlNodes.getAttribute("company")
Next
Thank you for your support! 
Kind regards,
Viktor
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|