Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP Classic - Parse XML InnerXML

Tags:

asp-classic

I like to be able to parsing the value like asp.net, unfortunately the asp classic also return the tag like the <div>everything within it</div> tag.

I am trying to parse the path "/root/div" and return everything within it, not including the "<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>
 %>
like image 964
James Smith Avatar asked Jun 17 '26 15:06

James Smith


2 Answers

Try this:

<%
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 the MSXML DOM elements do not have an innerXml property. Hence you need to the loop as exemplified about to concatenate the xml of each child node to genereate the inner XML of an element.

like image 117
AnthonyWJones Avatar answered Jun 19 '26 04:06

AnthonyWJones


This should get you what you're looking for:

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

I'm not sure about your exact case, but you might want to make sure that you have child nodes before assuming that, you can do it with:

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

Good luck!

like image 32
Adi Avatar answered Jun 19 '26 04:06

Adi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!