简体   繁体   English

如何使用VBA从xml文件的CDATA部分提取特定信息?

[英]How to extract specific information from CDATA section of an xml file with VBA?

My question is similar to what is described here . 我的问题与这里描述的相似。 However it is not for Excel-VBA. 但是,它不适用于Excel-VBA。

You will find that actually the text property resolves away the CDATA escape characters of <![CDATA[]]> . 您会发现text属性实际上解决了<![CDATA[]]>的CDATA转义字符。 So actually code is quite simple. 因此,实际上代码很简单。 Here is an example. 这是一个例子。

Sub Test()

    Dim sXml As String
    sXml = "<Root><SomeData>foo</SomeData>" & _
        "<SomeCDATA><![CDATA[<img src=""http://l.yimg.com/a/i/us/we/52/26.gif""/>]]>" & _
        "</SomeCDATA></Root>"
    'Tools->References->Microsoft Xml v.60
    Dim dom As MSXML2.DOMDocument60
    Set dom = New MSXML2.DOMDocument60
    dom.LoadXML sXml
    Debug.Assert dom.parseError = 0

    Dim xmlSomeCData As MSXML2.IXMLDOMElement
    Set xmlSomeCData = dom.SelectSingleNode("Root/SomeCDATA")

    Debug.Print xmlSomeCData.Text

    '* or your suggestion :)
    Dim xmlSomeCDataSection As MSXML2.IXMLDOMCDATASection
    Set xmlSomeCDataSection = dom.SelectSingleNode("Root/SomeCDATA/text()")

    Debug.Print xmlSomeCDataSection.Text

End Sub

the above code outputs <img src="http://l.yimg.com/a/i/us/we/52/26.gif"/> 上面的代码输出<img src="http://l.yimg.com/a/i/us/we/52/26.gif"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM