1

我需要解析一个 xmlobject 并从节点中提取两个值,我可以从这些树中获取两个值。我被困在如何向下遍历循环中的节点以获取一个值,然后确定我是否可以获取第二个值。

这是在 VBS 中,并被用作已经运行的程序中的扩展。一直在查看此处和 Microsoft 文档,但无法从它们那里获得有关 VBS 的良好信息。

示例 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"

如果元素的值为 1,我想从上面的 XML 中提取元素中的值。

编辑:修复了示例 xml 中放错位置的“/”

4

2 回答 2

1

我认为您获取类型和代码的 XPath 是错误的。尝试以下操作:

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
于 2019-10-01T04:23:04.353 回答
0

我能够通过收集我需要的节点来让它工作

xmlDoc.documentElement.selectNodes(strQuery)

然后用 for each 循环遍历那些。为此,我创建了一个附加函数,我传递objNode.xml给它并创建了一个附加的临时 xmlDOM,然后找到我需要的行是否在其中,如果是的话,将其拉出。

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"

我遇到的问题是在我尝试对我认为来自 IXMLDOMDocuments 的 IXMLDOMElement 使用选择方法之前。所以为了解决这个问题,我从 IXMLDOMElements 创建了新的 IXMLDOMDocuments。这就是我的想法。Idk 如果只有我,但是为此获取 VBS 文档很困难。我从 Microsoft 文档中获取了 javascript 或 c++ 代码位,但找不到显示 IXMLDOMElements 上的方法的表格。

于 2019-10-02T00:20:14.650 回答