c.Value是一个单元格中的文本(典型值是:名称,类型,重量,货币,但可以是任何东西)在工作表的第一行。
filenames是一个来自Dir()的循环,它基本上通过文件夹中的XML循环。
这些xmls可能具有最低级别的节点“权重”(例如),但在多个不同的父节点下。即这3种不同的xpath://客户/个人/重量和//客户/细节/重量和//客户/细节/信息/重量。
我有这段代码:
Dim aDoc As DOMDocument
Dim aNode As IXMLDOMNode
Set aDoc = LoadXmlDoc(filenames(f))
Set aNodes = aDoc.getElementsByTagName(c.Value)
For f = 1 To UBound(filenames)
If aNodes.Length > 0 Then 'if at least one node is present
For Each aNode In aNodes 'loop thru each occurence of a node
c.Offset(f, 0).Value = aNode.Text & "parent is " & aNode.parentNode.parentNode
Next
End If
Nxt f
我要做的是获取我在c.value中指定的子节点的xpath的明确列表。
例如,
if c.value is weight
我正试着把这个清单拿回来:
//Client/personal/weight
//Client/details/weight
//Client/details/info/weight.
http://msdn.microsoft.com/en-us/library/aa163921(office.10).aspx是一个很好的参考,但我似乎无法找到如何从子节点获取xpath。我只是设法得到了直接的父节点。
答案 0 :(得分:1)
您需要从节点开始并走近其父节点,直到达到文档级别。
Sub Tester()
Dim oDoc As New MSXML2.DOMDocument
Dim oNodes As MSXML2.IXMLDOMNodeList
Dim oNode As MSXML2.IXMLDOMNode
Dim pNode As MSXML2.IXMLDOMNode
Dim XML As String, sPath As String
XML = "<Client><LastName>Bill</LastName><FirstName>Gates</FirstName>" & _
"<MiddleName/><Suffix/><DateOfBirth>30-May-1968</DateOfBirth>" & _
"<PlaceOfBirth/><SSN>n/a</SSN><Gender>Male</Gender><District>" & _
"<City>SHELTON</City><Mayor>wong</Mayor></District><State>WA</State>" & _
"<Zip>96484</Zip></Client>"
oDoc.LoadXML XML
Set oNodes = oDoc.getElementsByTagName("City")
If oNodes.Length > 0 Then
For Each oNode In oNodes
sPath = oNode.nodeName
Set pNode = Nothing
Do
If pNode Is Nothing Then
Set pNode = oNode.ParentNode
Else
Set pNode = pNode.ParentNode
End If
If pNode.nodeTypeString <> "document" Then
sPath = pNode.nodeName & "/" & sPath
Else
sPath = "//" & sPath
Exit Do
End If
Loop
Debug.Print sPath
Next oNode
End If
End Sub