VBA XPath查找节点

时间:2015-05-21 18:26:12

标签: xml vba xpath

无法让XPath在下一行的下一行中为我工作:

Sub x()
Dim sXML As String
sXML = "<?xml version=""1.0"" standalone=""yes""?>"
sXML = "<NewDataSet><MyTable>"
sXML = sXML & "     <Active>true</Active>"
sXML = sXML & "     <SQLServer>APCD03</SQLServer>"
sXML = sXML & "     <SQLDatabase>OIS</SQLDatabase>"
sXML = sXML & " </MyTable>"
sXML = sXML & " <MyTable>"
sXML = sXML & "     <Active>false</Active>"
sXML = sXML & "     <SQLServer>APCD04</SQLServer>"
sXML = sXML & "     <SQLDatabase>OIS</SQLDatabase>"
sXML = sXML & " </MyTable></NewDataSet>"

Dim xDoc As New MSXML2.DOMDocument
Dim nodelist As IXMLDOMNodeList
Dim node As IXMLDOMNode
xDoc.loadXML (sXML)

Set nodelist = xDoc.documentElement.SelectNodes("MyTable") ' finds all nodes
Debug.Print nodelist(0).XML ' see example

Set node = xDoc.SelectSingleNode("MyTable[Active='true']") ' can't find 'Active' node
Debug.Print node.Text

End Sub

这是nodeList(0).XML:

<MyTable>
   <Active>true</Active>
   <SQLServer>APCD03</SQLServer>
   <SQLDatabase>OIS</SQLDatabase>
</MyTable>

如何使用SelectSingleNode()查找“活动”节点?

1 个答案:

答案 0 :(得分:0)

您错过了 irb(main):012:0> i1, i2 = 2,1 => [2, 1] irb(main):015:0> i1 ,i2 = i2, i1+i2 => [1, 3] irb(main):016:0> i1 => 1 irb(main):017:0> i2 => 3 irb(main):023:0> i1, i2 = 2,1 => [2, 1] irb(main):024:0> i1 = i2 => 1 irb(main):025:0> i2 = i1+i2 => 2 irb(main):026:0> i1 => 1 irb(main):027:0> i2 => 2 。你可以使用:

DocumentElement

Set node = xDoc.DocumentElement.SelectSingleNode("MyTable/Active[text()='true']")