Question XML Parsing

KAMPATEL1973

Member
Joined
Oct 7, 2015
Messages
11
Programming Experience
Beginner
Hi All,

I am hoping someone can shed some light on the following please:

1) I have an xml file as follows:

<typeNames xmlns="http://www.acme.com">
<typeName recordType="case" href="types/SAMPLECASE">SAMPLECASE</typeName>
<typeName recordType="source" href="types/SAMPLEST">SAMPLEST</typeName>
<typeName recordType="transaction" href="types/SAMPLEWT">SAMPLEWT</typeName>
</typeNames>

2) I am using the following code:

Dim xdoc As New XmlDocument
Dim xnodelist As XmlNodeList
Dim xNode As XmlNode
Dim XMLnode As XmlNode
Dim xAttr As XmlAttribute
Dim elem As XmlElement
Dim strlist As String
xdoc.LoadXml(xml)


xnodelist = xdoc.SelectNodes("//*/@*")
For Each xNode In xnodelist
strlist = strlist & " " & xNode.Value.ToString
Next


MsgBox(strlist)



This will return a string with no space as "case types/SAMPLECASE source types/SAMPLEST transaction types/SAMPLEWT. Not sure why it is not returning the Values in the > <?

I know by using:

doc = CreateObject("MSXML2.DOMDocument")
success = doc.loadXML(xml)


Work_Type_List = doc.text()

I can create a list which can then be used to create the following SAMPLECASE, SAMPLEST, SAMPLEWT which can be used to populate a list box.

What I am trying to get is a way of reading the XML file so I actually return:

case, SAMPLECASE
source, SAMPLEST
transaction, SAMPLEWT

Any ideas / help would be greatly appreciated.

Thanks

Kam
 
Not sure why it is not returning the Values in the > <?
Because you select all attribute nodes by @, see XPath Syntax

Select the typeName nodes, for each grab your values (the recordType attribute and the node value).
You also need to handle the namespace of the nodes, this is done with XmlNamespaceManager:
        Dim ns As New XmlNamespaceManager(xdoc.NameTable)
        ns.AddNamespace("ns", xdoc.DocumentElement.NamespaceURI)

Then you can select the typeName nodes from that namespace:
xnodelist = xdoc.DocumentElement.SelectNodes("ns:typeName", ns)

in loop get both values, for example:
 strlist &= xNode.Attributes("recordType").Value & ", " & xNode.InnerText & vbNewLine
 
The xml objects you use above in System.Xml namespace is getting old, they are from .Net 2.0 from 2005. With .Net 4 you can use the newer System.Xml.Linq objects, Linq to Xml, and xml literals that is much easier to work with.
Start with importing the namespace, this goes at the top of the code file:
Imports <xmlns="http://www.acme.com">

then you can read the xml into XDocument and loop and get values using Linq:
        Dim temp As String = String.Empty
        Dim xdoc = XDocument.Parse(xml)

        For Each node In xdoc...<typeName>
            temp &= node.@recordType & ", " & node.Value & vbNewLine
        Next

or the same code without importing the namespace in code file:
        Dim name = XName.Get("typeName", "http://www.acme.com")
        For Each node In xdoc.Descendants(name)
            temp &= node.@recordType & ", " & node.Value & vbNewLine
        Next
 
Back
Top