我有以下架构:
<import>
<node type="document" action="action">
<location>Spain:Development</location>
<title>Abono de factura</title>
<file>D:\OPENTEXT\12343fewf.pdf</file>
<category name="Content Server Categories:Non SAP Categories:Common:Migracion_Documentum">
<attribute name="Autor">ppieroni</attribute>
<attribute name="ID Documentum">E-0008749312</attribute>
</category>
</node>
<node type="document" action="action">
<location>Spain:Systems</location>
<title>Factura pendiente</title>
<file>D:\OPENTEXT\89443gs.xlsx</file>
<category name="Content Server Categories:Non SAP Categories:Common:Migracion_Documentum">
<attribute name="Autor">jcarballeira</attribute>
<attribute name="ID Documentum">I-0001245366</attribute>
</category>
</node>
</import>
当我按照此架构导入XML文件时,Excel会按如下所示布置数据:
https://i.ibb.co/8xj55jM/XML-mapping.jpg
由于我必须在映射“ Autor”或“ ID Documentum”之间进行选择,因此Excel似乎无法映射多个具有重复元素且具有不同名称标签的属性。
我需要能够映射多个具有不同属性和值的重复行。
我该如何实现?
致谢
答案 0 :(得分:0)
您可以使用XML解析器。
需要通过vbe>工具进行引用>对Micorsoft XML库的引用(对我而言是v.6)
Option Explicit
Public Sub test()
Dim xmlDoc As Object
Application.ScreenUpdating = False
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
With xmlDoc
.validateOnParse = True
.setProperty "SelectionLanguage", "XPath"
.async = False
If Not .Load("C:\Users\HarrisQ\Desktop\test.xml") Then
Err.Raise .parseError.ErrorCode, , .parseError.reason
End If
End With
Dim node As IXMLDOMElement, childNode As IXMLDOMElement, nextChildNode As IXMLDOMElement, r As Long, c As Long, attrib As Object
r = 1
For Each node In xmlDoc.SelectNodes("//node")
r = r + 1
With ActiveSheet
.Cells(r, 1) = node.getAttribute("type")
.Cells(r, 2) = node.getAttribute("action") '<== you can hardcode create here as varies from value of attribute
.Cells(r, 3) = node.SelectSingleNode("location").Text
.Cells(r, 4) = node.SelectSingleNode("title").Text
Set childNode = node.SelectSingleNode("category")
.Cells(r, 5) = childNode.getAttribute("name")
c = 6
For Each nextChildNode In childNode.SelectNodes("attribute")
.Cells(r, c) = nextChildNode.getAttribute("name")
c = c + 1
Next
End With
Next
Application.ScreenUpdating = True
End Sub