XML selectNodes using classic ASP

The XML problem that made me stop, but is probably very simple ...

XML looks like this:

    <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>

      

And the code I would like to use is:

    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

      

But I dont know how.

Also, it can be a problem, for example with tags <images>

and <links>

.

Suggestions please?

+2


source to share


2 answers


First, the XML example you posted is not valid. It has no root element (or has multiple root elements, depending on your point of view). Also, the element is <sleeps>

never closed. I think it might be typos in your example?

I'm not sure what you mean by "I want to be able to retrieve individual fields here". Can you give an example of the output you are after?

Without further information, I can suggest some variations on this:



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

      

Does it help?

+2


source


The code I use is:

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

      



I hope someone finds it useful!

+3


source







All Articles