简体   繁体   中英

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.

You will find that actually the text property resolves away the CDATA escape characters of <![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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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