ASP Classic - Parse XML InnerXML

I like to parse the value of asp.net, unfortunately, the asp classic also returns a tag, such as a tag <div>everything within it</div>.

I try to parse the path "/ root / div" and return everything to it, not including " <div></div>", the result should be " <p>abc <span>other span</span></p>"

<%
    Set CurrentXML=Server.CreateObject("Microsoft.XMLDOM")
    CurrentXML.LoadXml("<root><div><p>abc <span>other span</span></p></div></root>")'Load string into CurrentXML

    Response.Write("<BR>"&Server.HTMLEncode(CurrentXML.SelectSingleNode("/root/div").XML)) 'I want to return  without the div, the correct result should be <p>abc <span>other span</span></p>
 %>
+3
source share
2 answers

Try the following:

<%
Option Explicit

    Dim xml: Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0") 
    xml.LoadXml("<root><div>Text Start<p>abc <span>other span</span></p></div></root>")

    Dim sResult: sResult = ""

    Dim node
    For Each node in xml.selectSingleNode("/root/div").childNodes
        sResult = sResult & node.xml
    Next

    Response.Write Server.HTMLEncode(sResult) 

%>

Unfortunately, MSXML DOM elements do not have an innerXml property. Therefore, you will need a loop, as shown in the example, to combine the xml of each child element of the node to generate the internal XML of the element.

+2
source

This should get what you are looking for:

Response.Write("BR>"&Server.HTMLEncode(CurrentXML.SelectSingleNode("/root/div").childNodes.item(0).XML))

, , , ,

CurrentXML.SelectSingleNode("/root/div").hasChildNodes ' Should be "True"

!

+2

All Articles