【问题标题】:Import XML schema with repeated elements, but different attribute names导入具有重复元素但属性名称不同的 XML 模式
【发布时间】:2019-01-21 13:41:51
【问题描述】:

我有以下架构:

<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

似乎 Excel 无法映射多个具有不同名称标签的重复元素的属性,因为我必须在映射“Autor”或“ID Documentum”之间进行选择。

我需要能够映射多个具有不同属性和值的重复行。

我怎样才能做到这一点?

问候

【问题讨论】:

  • 当您说“属性”时,您是指 XML 意义上的属性,还是指具有名称“属性”的元素?

标签: excel xml xml-parsing


【解决方案1】:

您可以使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    相关资源
    最近更新 更多