Reputation:
I need to parse a xmlobject and extract from the nodes two values I am able to get two values from those trees. I am stuck on how to traverse down into the node in the loop to grab a value that then will determine if I can grab a second value.
This is in VBS and is being used as an extension in an already functioning program. Have been looking up here and Microsoft documentation but am having trouble getting good information from them for VBS in particular.
Example XML
<Detail>
<StandardLine>
<Stan>
<Type>A</Type>
<Code>1234</Code>
<Value>sdg</Value>
</Stan>
</StandardLine>
<StandardLine>
<Stan>
<Type>C</Type>
<Code>122234</Code>
<Value>sdsdgg</Value>
<Cate>Thiere</Cate>
</Stan>
</StandardLine>
<StandardLine>
<Stan>
<Type>1</Type>
<Code>7336</Code>
<Value>this one</Value>
<Stone>diamond</Stone>
</Stan>
</StandardLine>
</Detail>
Dim xmlDoc, nodes, strQuery, objNode
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
'This is inputing the xml as a string from a previous bit of code
xmlDoc.loadXML(XMLOut)
strQuery = "//Detail/StandardLine"
Set nodes = xmlDoc.documentElement.selectNodes(strQuery)
'Check to see if this xmlDoc has any of the desired SL nodes
If nodes.length = 0 Then
msgbox "There are no SLine nodes",,"The checker"
exit function
End If
'Will only get this far if there is a SLine node
msgbox "Before Nodes" & vbCrLf & nodes.length,,"Pre loop"
' Will go through each SLinenodes
For Each objNode in nodes
msgbox "I am in the loop!",,"The box...in the loop"
'Here down is what I want to do but am not able to get it to work
'Need to to process the SLine node
Type = objNode.documentElement.selectSingleNode("//Stan/Type").text
If Type = 1 Then
Code = objNode.documentElement.selectSingleNode("//Stan/Code").text
End if
'Will be functions here later to use the variable Code
msgbox "Number is: " & Code,,"Thereas the number?"
Next
msgbox "After Nodes",,"Post loop"
I want to extract from the above XML the value in the element if the value of the element is 1.
Edit: fixed a misplaced '/' in the example xml
Upvotes: 1
Views: 84
Reputation:
I was able to get it working by gathering the nodes I need with the
xmlDoc.documentElement.selectNodes(strQuery)
Then looping over those with a for each loop. For that I created an additional function that I passed objNode.xml
to and created an additional temporary xmlDOM and then found if the line I needed was in there and if so pulled it out.
Function ProcessXML(strXMl)
Dim xmlDoc,strQT, strQC,type,code, ty
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.loadXML(strXML)
strQT = "//StandardLine/Stan/Type"
strQC = "//StandardLine/Stan/Code"
set ty = xmlDoc.documentElement.selectSingleNode(strQT)
if not(ty is nothing) then
type = ty.text
else
type = -1
end if
if type = 1 then
code = xmlDoc.documentElement.selectSingleNode(strQC).text
else
Code = -1
end if
ProcessXML = Code
End Function
Dim xmlDoc, nodes, strQuery, objNode
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
'This is inputing the xml as a string from a previous bit of code
xmlDoc.loadXML(XMLOut)
strQuery = "//Detail/StandardLine"
Set nodes = xmlDoc.documentElement.selectNodes(strQuery)
'Check to see if this xmlDoc has any of the desired SL nodes
If nodes.length = 0 Then
msgbox "There are no SLine nodes",,"The checker"
exit function
End If
'Will only get this far if there is a SLine node
msgbox "Before Nodes" & vbCrLf & nodes.length,,"Pre loop"
' Will go through each SLinenodes
For Each objNode in nodes
Dim Code
msgbox "I am in the loop!",,"The box...in the loop"
Code = ProcessXML(objNode.xml)
'Will be functions here later to use the variable Code
msgbox "Number is: " & Code,,"Thereas the number?"
Next
msgbox "After Nodes",,"Post loop"
The problem I was having was before I was trying to use select methods on what was a IXMLDOMElement that are from IXMLDOMDocuments I think. So to get around that I created new IXMLDOMDocuments from the IXMLDOMElements. This is what I think. Idk if its just me but getting VBS documentation for this was difficult as f. I was getting javascript or c++ code bits from Microsoft documentation and couldn't find a table showing the methods on IXMLDOMElements.
Upvotes: 0
Reputation: 5031
I think your XPath to get the Type and Code was wrong. Try the following:
Private Sub Parse(p_sXML)
Dim xmldoc
Dim objNodes
Dim objNode
Dim sQuery
Dim sType
Dim sCode
Set xmldoc = CreateObject("MSXML2.DOMDocument.6.0")
xmldoc.Async = False
' This is inputing the xml as a string from a previous bit of code
xmldoc.loadXML p_sXML
sQuery = "//Detail/StandardLine"
Set objNodes = xmldoc.documentElement.selectNodes(sQuery)
' Check to see if this xmlDoc has any of the desired SL nodes
If objNodes.length = 0 Then
MsgBox "There are no SLine nodes", , "The checker"
Exit Sub
End If
' Will go through each nodes
For Each objNode In objNodes
' Get Type element
sType = objNode.selectSingleNode("Stan/Type").Text
If sType = "1" Then
sCode = objNode.selectSingleNode("Stan/Code").Text
End If
' Will be functions here later to use the variable Code
Next
End Sub
Upvotes: 1