3

我喜欢能够解析像 asp.net 这样的值,不幸的是 asp classic 也像标签一样返回<div>everything within it</div>标签。

我正在尝试解析路径“/root/div”并返回其中的所有内容,不包括“ <div></div>”,结果应该是“ <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>
 %>
4

2 回答 2

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.

于 2012-04-29T20:55:03.290 回答
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!

于 2012-04-29T20:57:48.067 回答