我有一个我试图解析的xml文件:
这是xml文件内容
<MYSTUFF>
<COMPANYNAMES>
<COMPANYNAME>JUMPIN (JIMMY) LIMITED</COMPANYNAME>
<COMPANYNAME>BLADE RUNNER'S TRANSPORT</COMPANYNAME>
<COMPANYNAME>P Griffiths & Sons</COMPANYNAME>
<COMPANYNAME>SOMETIMES, NEVER</COMPANYNAME>
<COMPANYNAME>MASTER/CLASS</COMPANYNAME>
</COMPANYNAMES>
<FIRSTNAMES>
<FIRSTNAME>Richard</FIRSTNAME>
<FIRSTNAME>Jo & hn</FIRSTNAME>
<FIRSTNAME>Paul</FIRSTNAME>
<FIRSTNAME>Geo, rge</FIRSTNAME>
<FIRSTNAME>Ringo</FIRSTNAME>
</FIRSTNAMES>
<LASTNAMES>
<LASTNAME>Davies'</LASTNAME>
<LASTNAME>Lennon</LASTNAME>
<LASTNAME>McCartney(3)</LASTNAME>
<LASTNAME>Harrison</LASTNAME>
<LASTNAME>St/ar</LASTNAME>
</LASTNAMES>
</MYSTUFF>
这是代码:
Dim XDoc As Object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load (ThisWorkbook.Path & "\test.xml")
'Get Document Elements
Set lists = XDoc.DocumentElement
'Traverse all elements 2 branches deep
For Each listNode In lists.ChildNodes
For Each fieldNode In listNode.ChildNodes
Debug.Print "[" & fieldNode.BaseName & "] = [" & fieldNode.Text & "]"
Next fieldNode
Next listNode
Set XDoc = Nothing
我正在使用或未在此行设置块获取对象变量:
For Each listNode In lists.ChildNodes
答案 0 :(得分:4)
您的XML文件无法正确加载。
a)我认为您的XML文件以<?xml version="1.0" encoding="utf-8"?>
之类的东西开头,因此可以将其标识为XML。
b)最好声明你的对象设置(总是在声明头中使用Option Explicit
)。当您使用所谓的后期绑定时,只需编写如下:
Dim XDoc As Object
Dim lists As Object
Dim listNode As Object
Dim fieldNode As Object
提示如果您使用Set XDoc = CreateObject("MSXML2.DOMDocument")
将XDoc对象设置为内存,通常会获得旧版本(3.0),因此在大多数情况下,最好使用Set XDoc = CreateObject("MSXML2.DOMDocument.6.0")
相反,它自动包含XPath。如果没有,您应按如下方式填写您的代码:
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.setProperty "SelectionLanguage", "XPath" ' << XPath functionality
c)您的XML文件未成功加载,因为它在 P Griffiths&amp;中包含一个名为&符号(“&amp;”)的不可读字符。儿子和 Jo&amp; hn ,必须更改为“&
”。 &符号用作特殊字符的通用前缀,因此不能单独包含在文件中。您可以使用以下代码测试加载,而不是仅使用XDoc.Load (ThisWorkbook.Path & "\test.xml")
:
If XDoc.Load(ThisWorkbook.Path & "\test.xml") Then
MsgBox "Loaded successfully"
Else
Dim xPE As Object ' Set xPE = CreateObject("MSXML2.IXMLDOMParseError")
Dim strErrText As String
Set xPE = XDoc.parseError
With xPE
strErrText = "Load error " & .ErrorCode & " xml file " & vbCrLf & _
Replace(.URL, "file:///", "") & vbCrLf & vbCrLf & _
xPE.reason & _
"Source Text: " & .srcText & vbCrLf & vbCrLf & _
"Line No.: " & .Line & vbCrLf & _
"Line Pos.: " & .linepos & vbCrLf & _
"File Pos.: " & .filepos & vbCrLf & vbCrLf
End With
MsgBox strErrText, vbExclamation
Set xPE = Nothing
Exit Sub
End If
d)顺便说一句,还有其他更完整的循环方式(递归调用)。当然你会在SO网站找到一些。
答案 1 :(得分:2)
你的代码对我来说很好。作为@FlorentB。建议,该文件可能无法解析。我怀疑它们是文件名中的错误。添加一些错误处理将有助于捕获这些错误。
Sub PrintXML()
Dim FilePath As String
Dim XDoc As Object
FilePath = ThisWorkbook.Path & "\test.xml"
If Len(Dir(FilePath)) = 0 Then
MsgBox "File not Found:" & vbCrLf & FilePath, vbCritical
Exit Sub
End If
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load ("C:\Users\norkiosk\Documents\Fax\test.xml")
'Get Document Elements
Set lists = XDoc.DocumentElement
If lists Is Nothing Then
MsgBox "Failed to Parse File:" & vbCrLf & FilePath, vbCritical
Exit Sub
End If
'Traverse all elements 2 branches deep
For Each listNode In lists.ChildNodes
For Each fieldNode In listNode.ChildNodes
Debug.Print "[" & fieldNode.BaseName & "] = [" & fieldNode.Text & "]"
Next fieldNode
Next listNode
Set XDoc = Nothing
End Sub