未设置带有或带有块的对象变量 [英] Object variable with or with block not set

查看:36
本文介绍了未设置带有或带有块的对象变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要解析的xml文件:

I have xml file which I am trying to parse:

这是xml文件的内容

This is the xml file content

<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

我正在使用此行上未设置的或带有块的对象变量:

I am getting object variable with or with block not set on this line:

For Each listNode In lists.ChildNodes

推荐答案

您的XML文件未正确加载.

Your XML file doesn't load correctly.

a)我想您的XML文件以类似<?xml version ="1.0" encoding ="utf-8"?> 的开头,因此可以将其标识为XML.

a) I suppose your XML file starts with something like <?xml version="1.0" encoding="utf-8"?>, so that it can be identified as XML.

b)最好声明您的对象设置(始终在声明头中使用 Option Explicit ).在使用所谓的后期绑定时,编写如下代码就足够了:

b) It's preferable to declare your object settings (always use Option Explicit in the declaration head). As you are using so called late binding, it's sufficient to write as follows:

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.如果没有,您应该按照以下步骤完成代码:

Hint If you set your XDoc object to memory with Set XDoc = CreateObject("MSXML2.DOMDocument") generally you are getting an older Version (3.0), so in most cases it's preferrable to use explicitly Set XDoc = CreateObject("MSXML2.DOMDocument.6.0") instead, which includes XPath automatically. If not you should complete your code as follows:

Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.setProperty "SelectionLanguage", "XPath"       ' << XPath functionality

c)您的XML文件未成功加载,因为它在 P Griffiths&儿子 Jo&hn ,必须将其更改为"&#38; ".&字符用作特殊字符的通用前缀,因此您不能单独包含在文件中.您可以使用以下代码测试加载,而不是简单地使用 XDoc.Load(ThisWorkbook.Path&"\ test.xml"):

c) Your XML file isn't loaded successfully because it contains a non readable character called ampersand ("&") within P Griffiths & Sons and Jo & hn, which has to be changed to "&#38;". The ampersand character is used as general prefix for special characters, so you can't be contained alone in the file. You can test loading with the following code instead of simply using 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网站上找到一些东西.

d) BTW, there are other and more complete ways to loop through your nodes (recursive calls). Sure you'll find some at the SO site.

这篇关于未设置带有或带有块的对象变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆