使用Classic ASP的XML selectNodes

时间:2009-09-16 13:28:55

标签: xml asp-classic

XML问题让我感到难过,但可能非常简单......

XML就像:

    <header>
    <createdOn>16 Sep 2009</createdOn>
    <createdBy>Jez</createdBy>
</header>
<agents>
    <agent>
            <agentDetails>
                    <agentName>text</agentName>
                    <agentTelephone>text</agentTelephone>
            </agentDetails>
            <properties>
                    <property>
                            <propertyid>number</propertyid>
                            <address>
                                    <number>1</number>
                                    <street>High St</street>
                                    <postcode></postcode>
                                    <country>UK</country>
                            </address>
                            <price>
                                    <category>text</category>
                                    <price>number</price>
                                    <reference>text</reference>
                            </price>
                            <description>
                                    <propertyType>House</propertyType>
                                    <bedrooms>2</bedrooms>
                                    <bathrooms>1</bathrooms>
                                    <sleeps>
                                    <briefDescription>text</briefDescription>
                                    <addDescription>long-text</addDescription>
                                    <floorSize>
                                            <size>80</size>
                                            <type>sq. mt</type>
                                    </floorSize>
                                    <bullets>
                                            <bullet>No Of Bedrooms : 2</bullet>
                                            <bullet>Condition : Habitable</bullet>
                                            <bullet>Land Size (M2): 2,000</bullet>
                                    </bullets>
                            </description>
                            <images>
                                    <image>
                                            <thumbnail>URL</thumbnail>
                                            <image>URL</image>
                                            <alttext></alttext>
                                    </image>
                                    <image>
                                            <thumbnail>URL</thumbnail>
                                            <image>URL</image>
                                            <alttext></alttext>
                                    </image>
                            </images>
                            <links>
                                    <link>
                                            <type>text</type>
                                            <url>url</url>
                                    </link>
                                    <link>
                                            <type>text</type>
                                            <url>url</url>
                                    </link>
                            </links>
                    </property>
            </properties>
    </agent>
 </agents>

我想使用的代码是:

    Set NodeList = objXML.documentElement.selectNodes("agents/agent/properties/property")
For Each Node In NodeList
    'I want to be able to extract distinct fields here...
    response.write Node.selectSingleNode("address/street") & "<br/>"
    response.write Node.selectSingleNode("description/briefDescription") & "<br/>"
Next

但是,我不知道如何。

另外,这可能是一个问题,例如<images><links>标签。

建议吗?

2 个答案:

答案 0 :(得分:3)

我正在使用的代码是:

Set NodeList = objXML.documentElement.selectNodes("agents/agent/properties/property")
For Each Node In NodeList
    Set AddrNode = Node.selectSingleNode("address/street/text()")
    if not AddrNode Is Nothing then response.write AddrNode.nodeValue & "<br/>"
    set AddrNode = nothing

    Set AddrNode = Node.selectSingleNode("address/region/text()")
    if not AddrNode Is Nothing then response.write AddrNode.nodeValue & "<br/>"
    set AddrNode = nothing

    For Each ImgNode In Node.selectNodes("images/image")
        Set ThNode = ImgNode.selectSingleNode("thumbnail/text()")
        if not ThNode Is Nothing then response.write ThNode.nodeValue & "<br/>"
        set ThNode = nothing
        Set ThNode = ImgNode.selectSingleNode("image/text()")
        if not ThNode Is Nothing then response.write ThNode.nodeValue & "<br/>"
        set ThNode = nothing
        Set ThNode = ImgNode.selectSingleNode("alttext/text()")
        if not ThNode Is Nothing then response.write ThNode.nodeValue & "<br/>"
        set ThNode = nothing
    next
Next

我希望其他人发现它有用!

答案 1 :(得分:2)

首先,您发布的XML示例无效。它缺少根元素(或具有多个根元素,具体取决于您的观点)。此外,<sleeps>元素永远不会关闭。我认为这些可能是你的例子中的拼写错误?

我不确定你的意思是“我希望能在这里提取不同的字段。”您能举例说明您的输出吗?

如果没有更多信息,我建议尝试一些变体:

Dim NodeList, Node, SubNode
'' # Note: Replace [root] with your actual root level element
Set NodeList = objXML.documentElement.selectNodes("/[root]/agents/agent/properties/property")
For Each Node In NodeList
    '' # Do something useful... ?? Distinct fields??
    Set Node = Node.selectSingleNode("address/street/text()")
    If Not Node Is Nothing Then
        Response.Write Server.HTMLEncode(Node.nodeValue) & "<br />"
    End If
Next

这有帮助吗?