3

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>
 %>
James Smith
  • 127
  • 2
  • 6

2 Answers2

2

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.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
2

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!

Adi
  • 707
  • 6
  • 12
  • This approach only works if the HTML fragment contained in the "div" only contains one xml child node. See my answer where I've injected a case where there is also a loose text node in there. – AnthonyWJones Apr 29 '12 at 21:03
  • Hi, thanks for noticing. You're right, I answered regarding the specific XML given as an example, this should be taken in consideration. – Adi Apr 29 '12 at 21:10
  • thanks guys, Adi simple solution solved my problem. Thank you. – James Smith Apr 29 '12 at 21:30