I am working on a program to load an XML file with namespaces. I have includes a portion of the file as strInput.
The root tag, Media contains the namespace. If I use it, I get no nodes returned. However, if I selectNodes with //*, it returns all nodes.
If I remove the references to namsSpace, the Media tag commented out, it returns the 4 nodes. I can also select the node Tag where name = Model.
Can some please explain what I am doing wrong?
Dim xmlDom As DOMDocument60
Dim xmlNodeList As IXMLDOMNodeList
Dim xmlNode As IXMLDOMNode
Dim strPath As String
Dim strInput As String
Dim strOutPut As String
Set xmlDom = New DOMDocument60
xmlDom.async = False
'=== It fails with this =========
strInput = "<Media xmlns=""http://XXXXXX.com/YYYYYY/media/1.2"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" identifier=""50003"" mediaType=""Book"" tenantKey=""MAN"">"
'=== It works with this ===================
'strInput = "<Media identifier=""50003"" mediaType=""Book"" tenantKey=""MAN"">"
strInput = strInput & " <Translation description="""" locale=""en"" name=""Coronado (96A4A)""/>"
strInput = strInput & " <Tag name=""Assembly_ID"" value=""50003""/>"
strInput = strInput & " <Tag name=""Model"" value=""96A4A-AP""/>"
strInput = strInput & " <Tag name=""Product Line"" value=""ATVFFT""/>"
strInput = strInput & " <Tag name=""Year"" value=""2201""/>"
strInput = strInput & "</Media>"
'Load the XML
xmlDom.loadXML strInput
xmlDom.validateOnParse = True
xmlDom.setProperty "SelectionLanguage", "XPath"
xmlDom.setProperty "SelectionNamespaces", "xmlns='http://XXXXXX.com/YYYYYY/media/1.2'"
xmlDom.setProperty "SelectionNamespaces", "xmlns:xs='http://www.w3.org/2001/XMLSchema'"
strPath = "/Media/Tag"
'strPath = "/Media/Tag[@name='Model']"
'strPath = "//*"
Set xmlNodeList = xmlDom.documentElement.selectNodes(strPath)
Debug.Print "Found " & xmlNodeList.length & " Node"
For Each xmlNode In xmlNodeList
strOutPut = strOutPut & "The Name of the node: " & xmlNode.baseName & vbCrLf
For lngCnt = 0 To xmlNode.Attributes.length - 1
strOutPut = strOutPut & " Attribute Name: " & xmlNode.Attributes(lngCnt).baseName & vbCrLf & " Text: " & xmlNode.Attributes(lngCnt).Text & vbCrLf
strOutPut = strOutPut & " =====================" & vbCrLf
Next
strOutPut = strOutPut & "====================================" & vbCrLf
Debug.Print strOutPut
Next
Set xmlDom = Nothing
The root tag, Media contains the namespace. If I use it, I get no nodes returned. However, if I selectNodes with //*, it returns all nodes.
If I remove the references to namsSpace, the Media tag commented out, it returns the 4 nodes. I can also select the node Tag where name = Model.
Can some please explain what I am doing wrong?
Dim xmlDom As DOMDocument60
Dim xmlNodeList As IXMLDOMNodeList
Dim xmlNode As IXMLDOMNode
Dim strPath As String
Dim strInput As String
Dim strOutPut As String
Set xmlDom = New DOMDocument60
xmlDom.async = False
'=== It fails with this =========
strInput = "<Media xmlns=""http://XXXXXX.com/YYYYYY/media/1.2"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" identifier=""50003"" mediaType=""Book"" tenantKey=""MAN"">"
'=== It works with this ===================
'strInput = "<Media identifier=""50003"" mediaType=""Book"" tenantKey=""MAN"">"
strInput = strInput & " <Translation description="""" locale=""en"" name=""Coronado (96A4A)""/>"
strInput = strInput & " <Tag name=""Assembly_ID"" value=""50003""/>"
strInput = strInput & " <Tag name=""Model"" value=""96A4A-AP""/>"
strInput = strInput & " <Tag name=""Product Line"" value=""ATVFFT""/>"
strInput = strInput & " <Tag name=""Year"" value=""2201""/>"
strInput = strInput & "</Media>"
'Load the XML
xmlDom.loadXML strInput
xmlDom.validateOnParse = True
xmlDom.setProperty "SelectionLanguage", "XPath"
xmlDom.setProperty "SelectionNamespaces", "xmlns='http://XXXXXX.com/YYYYYY/media/1.2'"
xmlDom.setProperty "SelectionNamespaces", "xmlns:xs='http://www.w3.org/2001/XMLSchema'"
strPath = "/Media/Tag"
'strPath = "/Media/Tag[@name='Model']"
'strPath = "//*"
Set xmlNodeList = xmlDom.documentElement.selectNodes(strPath)
Debug.Print "Found " & xmlNodeList.length & " Node"
For Each xmlNode In xmlNodeList
strOutPut = strOutPut & "The Name of the node: " & xmlNode.baseName & vbCrLf
For lngCnt = 0 To xmlNode.Attributes.length - 1
strOutPut = strOutPut & " Attribute Name: " & xmlNode.Attributes(lngCnt).baseName & vbCrLf & " Text: " & xmlNode.Attributes(lngCnt).Text & vbCrLf
strOutPut = strOutPut & " =====================" & vbCrLf
Next
strOutPut = strOutPut & "====================================" & vbCrLf
Debug.Print strOutPut
Next
Set xmlDom = Nothing