Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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/0/xml/13.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 VBA导入XML数据_Excel_Xml_Vba_Msxml - Fatal编程技术网

使用Excel VBA导入XML数据

使用Excel VBA导入XML数据,excel,xml,vba,msxml,Excel,Xml,Vba,Msxml,我正在尝试将特定数据从和XML文件导入Excel工作表 我使用的代码是这样的 Dim oXMLFile As New DOMDocument60 Dim books As IXMLDOMNodeList Dim results() As String Dim i As Integer, booksUBound As Integer Dim book As IXMLDOMNode, title As IXMLDOMNode, author As IXMLDOMNode oXMLFile.Load

我正在尝试将特定数据从和XML文件导入Excel工作表

我使用的代码是这样的

Dim oXMLFile As New DOMDocument60
Dim books As IXMLDOMNodeList
Dim results() As String
Dim i As Integer, booksUBound As Integer
Dim book As IXMLDOMNode, title As IXMLDOMNode, author As IXMLDOMNode

oXMLFile.Load "C:\example.xml"

Set books = oXMLFile.SelectNodes("/OUT_MESSAGE/LINES/OUT_MESSAGE_LINE")
booksUBound = books.Length - 1

ReDim results(booksUBound, 1)

For i = 0 To booksUBound
    Set book = books(i) 
    Set title = book.SelectSingleNode("C00")
    If Not title Is Nothing Then results(i, 0) = title.Text
Next

Dim wks As Worksheet
Set wks = ActiveSheet
wks.Range(wks.Cells(1, 1), wks.Cells(books.Length, 2)) = results
它适用于这个XML

<?xml version="1.0" encoding="UTF-8"?>
<OUT_MESSAGE>
 <LINES>
  <OUT_MESSAGE_LINE>
   <C00>1231231</C00>
   <C01>3213213</C01>
  </OUT_MESSAGE_LINE>
  <OUT_MESSAGE_LINE>
   <C00>1231234</C00>
   <C01>3213214</C01>
  </OUT_MESSAGE_LINE>
 </LINES>
</OUT_MESSAGE>

但这给了我一个运行时错误

如果有人知道我必须对原始代码做哪些更改,我将不胜感激。

这对我很有用:

Dim xDoc、节点、oNode
设置xDoc=CreateObject(“MSXML2.DOMDocument.6.0”)
'注意:在默认名称空间中添加了一个'x=',以便我们以后可以引用它
xDoc.setProperty“SelectionNamespaces”_
“xmlns:x='urn.randomaddress.com.schema.test_out_message'”
xDoc.LoadXML Sheet2.Range(“A4”).Value“从工作表加载XML”
'使用上面添加的“x”前缀
Set nodes=xDoc.SelectNodes(“/x:OUT\u MESSAGE/x:LINES/x:OUT\u MESSAGE\u LINE”)
Debug.Print节点。长度'=1
对于节点中的每个oNode
调试。打印oNode。选择SingleNode(“x:C00”)。NodeType值
Debug.Print oNode.SelectSingleNode(“x:OBJSTATE”).nodeTypedValue
等等
下一个oNode
使用此XML:


321312
12312312
张贴
接受^拒绝^
张贴

让什么起作用?你想干什么?从标记中获取文本?您需要显示更多实际的XML-我们无法从单个开始标记中告诉您任何内容。但是,看起来您正在处理“名称空间”,因此您需要让
oXMLFile
了解这些信息-这不会自动发生。例如,参见@TimWilliams,我添加了我的XML与另一个XML的比较。另一篇文章看起来是可行的,我正在尝试。如果你能发布一个实际XML的示例,那会更有用——如果你不熟悉XML,那么我们就不能确定你的“书”示例是否反映了你的实际结构。如果内容是机密的,那么只需将其混淆,但保留其结构。另外,如果您可以发布您尝试过的代码,而不是上一篇文章中的代码,这也会有所帮助。@TimWilliams,我更新了示例XML,并对代码进行了更改,基本上只是SelectNodes部分和要查找的标记的名称。如何从您的结果中导出?您可以编写代码将结果放入单元格,而不是使用Debug.Print或在发布的代码中使用数组方法。当我将数组方法从原始代码添加到您的代码中时,需要进行必要的更改,它不起作用。
<?xml version="1.0" encoding="UTF-8"?>
<OUT_MESSAGE xmlns="urn:randomaddress-com:schema:test_out_message" xmlns:xsi="http://www.randomurl.com/123">
 <LINES>
  <OUT_MESSAGE_LINE>
   <C00>1231231</C00>
   <C01>3213213</C01>
  </OUT_MESSAGE_LINE>
  <OUT_MESSAGE_LINE>
   <C00>1231234</C00>
   <C01>3213214</C01>
  </OUT_MESSAGE_LINE>
 </LINES>
</OUT_MESSAGE>
Set books = oXMLFile.SelectNodes("/OUT_MESSAGE/LINES/OUT_MESSAGE_LINE")
Set books = oXMLFile.SelectNodes("/OUT_MESSAGE xmlns='urn:randomaddress-com:schema:test_out_message' xmlns:xsi='http://www.randomurl.com/123'/LINES/OUT_MESSAGE_LINE")