How to read XML attributes using VBA to Excel? - excel

Here is my code..
<?xml version="1.0" ?>
<DTS:Executable xmlns:DTS="www.microsoft.com/abc" DTS:ExecutableType="xyz">
<DTS:Property DTS:Name="PackageFormatVersion">3</DTS:Property>
<DTS:Property DTS:Name="VersionComments" />
<DTS:Property DTS:Name="CreatorName">FirstUser</DTS:Property>
<DTS:Property DTS:Name="CreatorComputerName">MySystem</DTS:Property>
</DTS:Executable>
In this I am able to read Elements using "abc.baseName" and its value using "abc.Text".
It gives me result as
Property 3
Property
Property FirstUser
In this how can I read "PackageFormatVersion" as 3? i.e., I know some value is 3 but what that value is how could I know??
I mean I have to select which attribute I want to read.

Refer either to the element's .Text property or the .nodeTypeValue property :
Sub TestXML()
Dim xmlDoc As Object 'Or enable reference to Microsoft XML 6.0 and use: MSXML2.DOMDocument
Dim elements As Object
Dim el As Variant
Dim xml$
xml = "<?xml version=""1.0"" ?>"
xml = xml & "<DTS:Executable xmlns:DTS=""www.microsoft.com/abc"" DTS:ExecutableType=""xyz"">"
xml = xml & "<DTS:Property DTS:Name=""PackageFormatVersion"">3</DTS:Property>"
xml = xml & "<DTS:Property DTS:Name=""VersionComments"" />"
xml = xml & "<DTS:Property DTS:Name=""CreatorName"">FirstUser</DTS:Property>"
xml = xml & "<DTS:Property DTS:Name=""CreatorComputerName"">MySystem</DTS:Property>"
xml = xml & "</DTS:Executable>"
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
'## Use the LoadXML method to load a known XML string
xmlDoc.LoadXML xml
'## OR use the Load method to load xml string from a file location:
'xmlDoc.Load "C:\my_xml_filename.xml"
'## Get the elements matching the tag:
Set elements = xmlDoc.getElementsByTagName("DTS:Property")
'## Iterate over the elements and print their Text property
For Each el In elements
Debug.Print el.Text
'## Alternatively:
'Debug.Print el.nodeTypeValue
Next
End Sub
I know some value is 3 but what that value is how could I know??
You can review the objects in the Locals window, and examine their properties:
Here is an alternative, which seems clunkier to me than using the GetElementsByTagName but if you need to traverse the document, you could use something like this:
Sub TestXML2()
Dim xmlDoc As MSXML2.DOMDocument
Dim xmlNodes As MSXML2.IXMLDOMNodeList
Dim xNode As MSXML2.IXMLDOMNode
Dim cNode As MSXML2.IXMLDOMNode
Dim el As Variant
Dim xml$
xml = "<?xml version=""1.0"" ?>"
xml = xml & "<DTS:Executable xmlns:DTS=""www.microsoft.com/abc"" DTS:ExecutableType=""xyz"">"
xml = xml & "<DTS:Property DTS:Name=""PackageFormatVersion"">3</DTS:Property>"
xml = xml & "<DTS:Property DTS:Name=""VersionComments"" />"
xml = xml & "<DTS:Property DTS:Name=""CreatorName"">FirstUser</DTS:Property>"
xml = xml & "<DTS:Property DTS:Name=""CreatorComputerName"">MySystem</DTS:Property>"
xml = xml & "</DTS:Executable>"
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
'## Use the LoadXML method to load a known XML string
xmlDoc.LoadXML xml
'## OR use the Load method to load xml string from a file location:
'xmlDoc.Load "C:\my_xml_filename.xml"
'## Get the elements matching the tag:
Set xmlNodes = xmlDoc.ChildNodes
'## Iterate over the elements and print their Text property
For Each xNode In xmlDoc.ChildNodes
If xNode.NodeType = 1 Then ' only look at type=NODE_ELEMENT
For Each cNode In xNode.ChildNodes
Debug.Print cNode.nodeTypedValue
Debug.Print cNode.Text
Next
End If
Next
End Sub

Sub TestXML()
Set Reference to Microsoft XML 6.0
Dim Init As Integer
Dim xmlDoc As MSXML2.DOMDocument
Dim elements As Object
Dim el As Variant
Dim Prop As String
Dim NumberOfElements As Integer
Dim n As IXMLDOMNode
Init = 5
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.Load ("C:\Users\Saashu\Testing.xml")
Set elements = xmlDoc.getElementsByTagName("DTS:Property")
Prop = xmlDoc.SelectSingleNode("//DTS:Property").Attributes.getNamedItem("DTS:Name").Text
NumberOfElements = xmlDoc.getElementsByTagName("DTS:Property").Length
For Each n In xmlDoc.SelectNodes("//DTS:Property")
Prop = n.Attributes.getNamedItem("DTS:Name").Text
Prop = Prop & " :: " & n.Text
ActiveSheet.Cells(Init, 9).Value = Prop
Init = Init + 1
Next
End Sub
This code still needs refinement as my requirement is to display only some of those attributes like CreatorName and CreatorComputerName,not all.
Thanks to David,for helping me in this issue.

Related

XML and XPath handling in VBA

I'm trying to parse a XML into a spreadsheet using VBA, and for some reason I can't to the node that I want using XPath, here how my XML looks like:
<?xml version="1.0" encoding="UTF-8"?>
<cteProc xmlns="http://www.somesite.com" versao="3.00">
<CTe xmlns="http://www.somesite.com">
<infCte Id="an id" versao="3.00">
<ide>
<cUF>23</cUF>
<cCT>00000557</cCT>
<CFOP>6932</CFOP>
<natOp>some text </natOp>
<mod>57</mod>
</ide>
<compl>
<xObs>TEXT</xObs>
</compl>
</infCte>
</CTe>
</cteProc>
I'm trying to get at least to the ide node, so I can loop over the rest and get the information I want.
My code looks like this:
Public Sub parseXml()
Dim oXMLFile As MSXML2.DOMDocument60
Dim nodes As MSXML2.IXMLDOMNodeList
path2 = "C:\Users\me\Desktop\adoc.xml"
Set oXMLFile = New MSXML2.DOMDocument60
oXMLFile.Load (path2)
Set nodes = oXMLFile.DocumentElement.SelectNodes("/CTe")
So I tried to print the length of the nodes, I get this:
debug.print nodes.length
> 0
if I loop over like this:
Public Sub parseXml()
Dim oXMLFile As MSXML2.DOMDocument60
Dim nodes As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
path2 = "C:\Users\me\Desktop\adoc.xml"
Set oXMLFile = New MSXML2.DOMDocument60
oXMLFile.Load (path2)
Set nodes = oXMLFile.DocumentElement.ChildNodes
For Each node In nodes
Debug.Print node.BaseName
Next node
I get this:
> CTe
So, If I do a giant loop I can get the information I want, but I think there must be a simpler sulution for this.
Since your XML uses namespaces, XPath also needs to deal with namespaces.
The following works for me using your XML:
Public Sub parseXml()
Dim oXML As MSXML2.DOMDocument60
Dim oNodes As MSXML2.IXMLDOMNodeList
Dim oItem As MSXML2.IXMLDOMNode
Dim path2 As String
path2 = "P:\adoc.xml"
Set oXML = New MSXML2.DOMDocument60
oXML.Load path2
oXML.setProperty "SelectionLanguage", "XPath"
oXML.setProperty "SelectionNamespaces", "xmlns:ssc=""http://www.somesite.com"""
Set oNodes = oXML.DocumentElement.SelectNodes("ssc:CTe")
For Each oItem In oNodes
MsgBox oItem.nodeName
Next
End Sub
There using
oXMLFile.setProperty "SelectionNamespaces", "xmlns:ssc=""http://www.somesite.com"""
I define a prefix ssc for the namespace
http://www.somesite.com.
The scc is my own choice (somesite.com). This prefix is needed for the XPATH in selectNodes method to work properly.
If you don't want defining the namespace, you would must use the local-name() XPath function. For example:
Set oNodes = oXML.DocumentElement.SelectNodes("*[local-name() = 'CTe']")

VBA - What's The XPath Syntax To Get Tag Names

I am trying to use a VBA macro to parse XML file. Given with the following structure:
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
How can I enumerate the output with element tags with its corresponding values as shown below?
book | category | children
title | harry potter
author | J K. Rowling
...
My code as follows:
Set xmlFile = CreateObject("Microsoft.XMLDOM")
xmlFile.Load (file)
Set qXML = xmlFile.SelectNodes("/bookstore")
For i = 0 To qXML.Length - 1
Debug.Print CStr(qXML(i).Text)
Next i
How to get Tag Names
"What's the XPath syntax to get Tag Names?"
Strictly speaking it's (XML)DOM syntax to to get .Name and/or .NodeName properties;
XMLDOM (Document Object Model) is a cross-platform and language-independent interface treating the document as a tree structure and allowing programmatic access to the tree.
You can use, however the special syntax of XPath expressions (like e.g. "/bookstore/book/title") to address any logical part in the hierarchical xml document structure.
So a solution close to your OP would be:
Option Explicit ' declaration head of your code module
Sub ExampleCall()
Dim file As String: file = ThisWorkbook.Path & "\xml\bookstore.xml"
Dim xmlFile As Object
Set xmlFile = CreateObject("Microsoft.XMLDOM")
If xmlFile.Load(file) Then
Dim qXML As Object
Set qXML = xmlFile.DocumentElement.SelectNodes("book")
Dim q As Object
For Each q In qXML
Dim cnt As Long: cnt = cnt + 1
Debug.Print Format(cnt, "--- 000 ---")
Debug.Print q.Attributes(0).Name, "|" & q.Attributes(0).Text
Dim i As Long
For i = 0 To q.ChildNodes.Length - 1
Debug.Print q.ChildNodes(i).nodeName, "|" & q.ChildNodes(i).Text
Next
Next
End If
End Sub
Results in VBE's immediate window
--- 01 ---
category |children
title |Harry Potter
author |J K. Rowling
year |2005
price |29.99
--- 02 ---
category |web
title |Learning XML
author |Erik T. Ray
year |2003
price |39.95
Side note
As Microsoft.XMLDOM has been deprecated for years,
I'd prefer binding to ►MSXML2 in the most current xml version Microsoft XML,v6.0, e.g. via
I. LATE Binding (as in OP)
Dim xDoc As Object
Set xDoc = CreateObject("MSXML2.DOMDocument.6.0")
II. EARLY Binding
Dim xDoc As MSXML2.DOMDocument60 ' *) whereas MSXML2.DOMDocument (=old version 3.0)
Set xDoc = New MSXML2.DOMDocument60 ' mind the missing point in digits
Side note: OP uses the object variable XMLFile instead of xDoc
Note that referencing DOMDocument without obvious versioning would bind internally to 3.0 by default
(the last stable version before 6.0, any other versions are deprecated).
Further links
Obtain attribute names from xml using VBA
XML Parse ...
I suggest to use early binding.
So in your project in the VBE add a reference (menu tools/references) to Microsoft XML, v6.0.
To determine the attribute and the values you can use this:
Dim xmlFile As MSXML2.DOMDocument60
Set xmlFile = New MSXML2.DOMDocument60
xmlFile.Load file
Dim qXML As MSXML2.IXMLDOMNodeList
Set qXML = xmlFile.SelectNodes("/bookstore/book")
Dim index As Long
For index = 0 To qXML.Length - 1
Debug.Print qXML(index).SelectSingleNode("#category").Text
Debug.Print qXML(index).SelectSingleNode("title").Text
Debug.Print qXML(index).SelectSingleNode("author").Text
Next index

Using Xpath in Excel to read a child node's attribute

I am having problems getting the value of "Data Import" from the source_lvl2 child node. In Excel, I get a run-time error of 91, "Object variable or With block variable not set"
I can't see what I'm doing wrong - any advice?
VBA
Sub TestXML()
Dim XDoc As Object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load ("C:\171215-000438_1513346972.xml")
Set lists = XDoc.SelectNodes("/archive/primary_rnw_contact/source/source_lvl2")
Debug.Print lists(0).Attributes(0).Text
Set XDoc = Nothing
End Sub
XML
EXTRA CODE
<?xml version="1.1" encoding="UTF-8"?>
<archive product="RightNow" version="4.0" build="17.8.0.1.0.248" label="Archived Incident">
You could try the following (not tested):
Dim xmap As XmlMap
Dim k as Long
Dim oMyNewColumn As ListColumn
oMyList As ListObject
' Delete all previous XML maps
k = ThisWorkbook.XmlMaps.Count 'Detect all XML maps
For i = 1 To k
ThisWorkbook.XmlMaps(i).Delete
Next i
...
Set xmap = ThisWorkbook.XmlMaps.Add("some_file.xml")
...
Set oMyNewColumn = oMyList.ListColumns.Add
oMyList.ListColumns(3).XPath.SetValue xmap, "/archive/primary_rnw_contact/source/source_lvl2"

How to read a XML self-closing tag in Excel VBA?

I have a XML file that contains a tag that can either have text or can be self-closing when it has no text.
Case 1 (with text):
<Example>
<size>512</size>
</Example>
Case 2 (no text - self-closing):
<Example>
<size />
</Example>
I want to read this tag text in Excel VBA.
In Case 1, no problem, I do the following:
Set oXMLFile = CreateObject("Microsoft.XMLDOM")
oXMLFile.Load ("File.xml")
size = oXMLFile.SelectSingleNode("/Example/size/text()").NodeValue
But in Case 2, the SelectSingleNode function returns this error:
Run-time error '438':
Object doesn't support this property or method
How can I handle Case 2 so that it returns me an empty string? Is there a built-in VBA function to test if a tag is self-closing?
Option Explicit
Sub Test()
Dim oXMLFile As Object
Dim oNode As Object
Dim sSize As String
Set oXMLFile = CreateObject("MSXML2.DOMDocument.6.0")
oXMLFile.LoadXML "<Example><size>512</size></Example>"
Set oNode = oXMLFile.SelectSingleNode("/Example/size/text()")
If Not oNode Is Nothing Then sSize = oNode.NodeValue
End Sub

Unable to extract value for xml tags

I am a new to vba and have a task to do at hand. I have written some unction but could not debug it properly. I have following string in xml format:
<!--?xml version=""1.0"" encoding=""UTF-8""?-->
<credentials>
<mid>P</mid>
<mpid>Q</mpid>
<accid>R</accid>
<accesskey>S</accesskey>
<secretkey>T</secretkey>
</credentials>
and am trying to extract value corresponding to tag mid, mpid, accid etc.
Here is the subroutine I wrote:
Private Sub ExtractMWSCredentials(Text As String
Set xmldoc = Nothing
DoEvents
Set xmldoc = Get_XML_DOMDocument_Object()
xmldoc.LoadXML (Trim(Text))
Set mId = xmldoc.getElementsByTagName("mid").Item(0).Text
Set mpId = xmldoc.getElementsByTagName("mpid").Item(0).Text
Set accid = xmldoc.getElementsByTagName("accid").Item(0).Text
Set accesskey = xmldoc.getElementsByTagName("accesskey").Item(0).Text
End Sub
Everything is coming out to be empty. Any help appreciated.
I can't even get that code to compile. You don't use the Set keyword to store a non-object value like Text. You could code, for instance,
Set omId = xmldoc.getElementsByTagName("mid").Item(0)
Debug.Print omId.Text
Also, mId is a reserved word so it can't be a variable name. Here's one way you could do it.
Public Sub Extract()
Dim xDoc As MSXML2.DOMDocument60
Dim sMid As String, sMpid As String
Dim sAccid As String, sAccessKey As String
Set xDoc = New MSXML2.DOMDocument60
xDoc.LoadXML "<!--?xml version=""1.0"" encoding=""UTF-8""?-->" & _
"<credentials><mid>P</mid><mpid>Q</mpid><accid>R</accid>" & _
"<accesskey>S</accesskey><secretkey>T</secretkey></credentials>"
sMid = xDoc.getElementsByTagName("mid").Item(0).Text
sMpid = xDoc.getElementsByTagName("mpid").Item(0).Text
sAccid = xDoc.getElementsByTagName("accid").Item(0).Text
sAccessKey = xDoc.getElementsByTagName("accesskey").Item(0).Text
Debug.Print sMid, sMpid, sAccid, sAccessKey
End Sub
I don't use Set because I'm not storing the Item() in a variable, I'm storing the Item().Text in a variable.

Resources