dongmai6666 2017-04-06 07:06
浏览 208
已采纳

XML解析特殊字符:Excel VBA运行时错误91

When XML parsing special characters - my specific issue is with ampersands - Excel VBA is throwing the error

Run time error 91: Object variable or With block variable not set

PHP code for XML Parsing:

$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<root> <EngineerFName>'.$engineer_fname.'</EngineerFName>
<CustomerName>'.$customer_name.'</CustomerName>
<EngineerLName>'.$engineer_lname.'</EngineerLName>
<TopName>'.$dt_top_name.'</TopName>  
</root>';

Excel Vba code:

Dim XDoc1 As Object
Dim XHTML1 As IHTMLElement
Dim XURL1 As String
Dim CustomerName1 As String
Set XDoc1 = CreateObject("MSXML2.DOMDocument")
XDoc1.async = False: XDoc1.validateOnParse = False
XDoc1.Load ("http://www.beamon.com/windows_application/macro2.php" +  "?Id=" + Sheets(1).Range("D2"))
Set lists = XDoc1.DocumentElement
Set getFirstChild = lists.FirstChild
Set getCustomerName1 = lists.ChildNodes(1)
Set getEnglname = lists.ChildNodes(2)
Set getTopCustomer = lists.ChildNodes(3)

Sheets(1).Range("T5") = getCustomerName1.text
Sheets(1).Range("T6") = getFirstChild.text & Space(1) & getEnglname.text
Sheets(1).Range("T7") = getTopCustomer.text
Set XDoc1 = Nothing

Can anyone suggest a solution?

  • 写回答

1条回答 默认 最新

  • duanchouyi6730 2017-04-06 09:35
    关注

    There's a good article on your problem at Techrepublic - quoting the necessary part:

    When the XML parser finds an ampersand in the XML data, it expects to find a symbol name and a semicolon following it. The symbol name provides a symbolic reference to another entity or character such as the ampersand, greater-than, and less-than characters. The symbolic name for greater-than is gt and for less-than is lt. To include a greater-than character in the XML data, you must use the following syntax: &gt;

    If you already had the response from the PHP page then the solution to your problem is simply to do this:

    strXml = VBA.Replace(strXml, "&", "&amp;")
    

    But your issue is that you are using the Load method of the DOMDocument class and the PHP is emitting invalid XML. The PHP page should do this encoding for you - my guess is that it just queries some data store and slots it into an XML string an echos it without doing any validation on the values. Your Load method will not error, but the moment you want to parse the DOM, you get the problem.

    Given you are already referencing the MSXML library, perhaps your option is to use the XMLHTTP class to get the response, do the replacement yourself, and then load it the DOMDocument using the LoadXML method. See the code below - it is not tested as I don't know the parameter for your URL:

    Option Explicit
    
    Sub TextXMLGetAndParse()
    
        Dim strUrl As String
        Dim objXhr As MSXML2.XMLHTTP
        Dim strXml As String
        Dim objXmlDoc As MSXML2.DOMDocument
    
        Set objXhr = New MSXML2.XMLHTTP
        Set objXmlDoc = New MSXML2.DOMDocument
    
        ' do a XHR GET to your URL
        strUrl = "http://www.beamon.com/windows_application/macro2.php" + "?Id=" + Sheets(1).Range("D2")
        With objXhr
            .Open "GET", strUrl, False
            .send
            strXml = .responseXML
        End With
    
        ' do the clean-up that the PHP page should do for you
        strXml = VBA.Replace(strXml, "&", "&amp;")
    
        ' load that XML to you DOMDOcument
        objXmlDoc.LoadXML strXml
    
        ' check values
        Debug.Print objXmlDoc.DocumentElement.Text
        Debug.Print objXmlDoc.DocumentElement.FirstChild.Text
        Debug.Print objXmlDoc.DocumentElement.ChildNodes(1).Text
        Debug.Print objXmlDoc.DocumentElement.ChildNodes(2).Text
        Debug.Print objXmlDoc.DocumentElement.ChildNodes(3).Text
    
        ' clean up
        Set objXhr = Nothing
        Set objXhr = Nothing
    
    End Sub
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 高有偿提问!求优化设计微信小程序
  • ¥15 matlab在安装时报错 无法找到入口 无法定位程序输入点
  • ¥15 收益高的广告联盟有哪些
  • ¥15 Android Studio webview 的使用问题, 播放器横屏全屏
  • ¥15 删掉jdk后重新下载,Java web所需要的eclipse无法使用
  • ¥15 uniapp正式环境中通过webapi将本地数据推送到设备出现的跨域问题
  • ¥15 xui建立节点,显示错误
  • ¥15 关于#单片机#的问题:开始、复位、十进制的功能可以实现,但是切换八进制的功能无法实现(按下按键也没有效果),把初始状态调成八进制,也是八进制可以实现但是切换到十进制不行(相关搜索:汇编语言|计数器)
  • ¥15 VINS-Mono或Fusion中feature_manager中estimated_depth是特征的深度还是逆深度?
  • ¥15 谷歌浏览器如何备份抖音网页数据