Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有开源工具或excel宏从xml中提取XPath列表?_Xml_Excel_Vba_Xpath - Fatal编程技术网

有没有开源工具或excel宏从xml中提取XPath列表?

有没有开源工具或excel宏从xml中提取XPath列表?,xml,excel,vba,xpath,Xml,Excel,Vba,Xpath,基本上,我希望能够指定一个xml或类似的2(如果您可以选择一个文件夹,它将从那里获取所有xml文件,那就太好了): Xml 1: <Client> <LastName>Bill</LastName> <FirstName>Gates</FirstName> <MiddleName/> <Suffix/>

基本上,我希望能够指定一个xml或类似的2(如果您可以选择一个文件夹,它将从那里获取所有xml文件,那就太好了):

Xml 1:

<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>
所需行为:在新excel工作表(即“结果”)的表格中接收结果,其中包含以下列:

/Client/DateOfBirth  /Client/Account/Type/Name   /Client/Zip   /Client/District/City

----------------     ------------------------    ------------   --------------------
30-May-1968                                      96484              SHELTON
12-Aug-1957           Cash                       66096
我不介意是否使用excel vba宏完成此操作

更新-调试Tim的答案:

您只需将Xml映射添加到工作簿中,并将所需元素拉入工作表即可:

  • 使用Excel打开Xml源文件

  • 选择“使用Xml源任务窗格”选项并单击“确定”

  • 接受Excel将为您创建架构

  • 将所需元素从Xml源窗格拖到列中

  • 右键单击任意列并选择“Xml”->“导入”

  • 打开所需的Xml文件

  • 你可以用这个

    要将xml传输到列,请使用

                If adoRS.Fields(ndx).Type = adChapter Then
                    'you have a new column
                    Set adoChildRS = adoRS.Fields(ndx).Value
                    WalkHier iLevel, adoChildRS
                Else
                    'put value in column
                    Debug.Print iLevel & ": adoRS.Fields(" & ndx & _
                       ") = " & adoRS.Fields(ndx).Name & " = " & _
                       adoRS.Fields(ndx).Value
                End If
    

    使用您的示例(在顶部添加了xml DTD),这对我来说很有效

    子测试仪()
    ProcessFilesThisWorkbook.Path,Sheet1.Range(“A1:D1”)
    端接头
    子进程文件(FolderPath作为字符串,XPathRange作为范围)
    Dim-oXML作为MSXML2.DOMDocument
    Dim oNode作为MSXML2.IXMLDOMNode
    作为字符串的Dim fName
    调光范围
    作为整数的Dim x
    昏暗的房车
    x=1
    fName=Dir(FolderPath&“\*.xml”)
    当fName“”时执行
    设置oXML=New MSXML2.DOMDocument
    加载文件夹路径&“\”&fName
    对于XPathRange.Cells中的每个c
    rv=“”
    设置oNode=oXML。选择singlenode(c.Value)
    如果不是oNode,那就什么都不是了
    rv=oNode.nodeTypedValue
    如果结束
    c、 偏移量(x,0)。值=rv
    下一个c
    x=x+1
    fName=Dir()
    环
    端接头
    
    操作过于繁琐-我需要能够只单击一个按钮并返回我指定的xpath。您可以准备一份包含所有步骤1-5的工作簿,并记录一个宏,以便在单击按钮时执行步骤5+6。您可以将xpath与msxml实例一起使用,并使用Dir()循环遍历文件夹中的所有XML文件。您的示例XML没有根元素,因此如果这是整个内容,则可能会导致问题。您要求很多。到目前为止你有没有写过代码?正如Tim提到的,没有根元素,因此文件无法正确解析。它们实际上是2个XML,客户端是根元素。这个问题严重未指定。您只显示一个XML文档(格式不正确)。请提供至少两个XML文档以及结果,现在必须猜测…@Dimitre Novatchev-请参阅edit.good start-1。需要某种方法来捕获整个xpath,而不仅仅是子标记。2.提供要写入工作表的循环。3.为OpenFileDialog提供一个工具,您可以在其中添加1个或多个XML。如果你做了3个中的2个,那么悬赏、接受答案和追加投票就是你的了。看一下我在问题中发布的屏幕截图-似乎找不到xml文件,因为在我运行Tester Sub时Msgbox“entry”从未执行过。然后我尝试手动执行oXML.Load“C:\xpathtest2\t1.xml”我收到了以下错误:“Automation error unspecified error”您是否在项目中添加了对MSXML的引用?我已使用v4和v6进行了测试。如果设置了此选项,则可能是您的XML文件有问题。或者可以尝试关闭Norton AV。
    /Client/DateOfBirth  /Client/Account/Type/Name   /Client/Zip   /Client/District/City
    
    ----------------     ------------------------    ------------   --------------------
    30-May-1968                                      96484              SHELTON
    12-Aug-1957           Cash                       66096
    
                If adoRS.Fields(ndx).Type = adChapter Then
                    'you have a new column
                    Set adoChildRS = adoRS.Fields(ndx).Value
                    WalkHier iLevel, adoChildRS
                Else
                    'put value in column
                    Debug.Print iLevel & ": adoRS.Fields(" & ndx & _
                       ") = " & adoRS.Fields(ndx).Name & " = " & _
                       adoRS.Fields(ndx).Value
                End If
    
    Sub Tester()
        ProcessFiles ThisWorkbook.Path, Sheet1.Range("A1:D1")
    End Sub
    
    Sub ProcessFiles(FolderPath As String, XPathRange As Range)
        Dim oXML As MSXML2.DOMDocument
        Dim oNode As MSXML2.IXMLDOMNode
        Dim fName As String
        Dim c As Range
        Dim x As Integer
        Dim rv
    
        x = 1
        fName = Dir(FolderPath & "\*.xml")
        Do While fName <> ""
            Set oXML = New MSXML2.DOMDocument
            oXML.Load FolderPath & "\" & fName
    
            For Each c In XPathRange.Cells
                rv = ""
                Set oNode = oXML.SelectSingleNode(c.Value)
                If Not oNode Is Nothing Then
                    rv = oNode.nodeTypedValue
                End If
                c.Offset(x, 0).Value = rv
            Next c
    
            x = x + 1
            fName = Dir()
        Loop
    
    End Sub