1
votes

I have an XML file with nodes and attributes. I am accessing and receiving data from XML with classic ASP. But XML files has some attributes I should print on the screen.

The XML file is something like

<root>
<product>
<node1>Node1 Value</node1>
<node2>Node2 Value</node2>
<attribute value="category">Category Name</attribute>
</product>
</root>

And I am receiving the data with this script

Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
objXMLDoc.async = True
objXMLDoc.load Server.MapPath("ProductList3.xml")

Dim xmlProduct
For Each xmlProduct In objXMLDoc.documentElement.selectNodes("product")

     Dim node1 : node1 = xmlProduct.selectSingleNode("node1").text
     Dim node2 : node2 = xmlProduct.selectSingleNode("node2").text

     Response.Write "<b>node1:</b>" & Server.HTMLEncode(node1) & "<br> "
     Response.Write "<b>node2:</b>" & Server.HTMLEncode(node2) & "<br>"   %>
Next

I don't have any problem accessing the nodes but i need to get attribute value "category", so i tried something like

Dim category : Set category = getText(xmlProduct.SelectNodes("root/product/attribute value[@name='category']")

But i am receiving ")" required in line 52 error (err no:800a03ee).

Set category= getText(xmlProduct.SelectNodes("root/attribute value[@name='Category']")

I must get the category name in the attribute but couldn't find any solution, maybe I am completely wrong in line 52. Can you guys help me to solve the issue?

1
There is no node named urun in your XML. Please double check that your sample code matches your sample input document. - Tomalak

1 Answers

1
votes
Dim productList, product, node1, node2, category

Set productList = Server.CreateObject("MSXML2.DOMDocument.3.0")
productList.async = False  ' you don't want async document loading on the server
productList.load Server.MapPath("ProductList3.xml")

For Each product In productList.selectNodes("/root/product")
     Set node1 = product.selectSingleNode("./node1")
     Set node2 = product.selectSingleNode("./node2")
     Set category = product.selectSingleNode("./attribute[@value='category']")

     Response.Write "<b>node1:</b>" & Server.HTMLEncode(node1.text) & "<br>"
     Response.Write "<b>node2:</b>" & Server.HTMLEncode(node2.text) & "<br>"
     Response.Write "<b>category:</b>" & Server.HTMLEncode(category.text) & "<br>"
Next

Since it's not very smart to select nodes and use their properties without knowing whether that node actually exists (selectSingleNode can return Nothing, which will lead to run-time errors in the code above), the following is much safer to use:

For Each product In productList.selectNodes("/root/product")
     node1 = GetText(product, "./node1")
     node2 = GetText(product, "./node2")
     category = GetText(product, "./attribute[@value='category']")

     Response.Write "<b>node1:</b>" & Server.HTMLEncode(node1) & "<br>"
     Response.Write "<b>node2:</b>" & Server.HTMLEncode(node2) & "<br>"
     Response.Write "<b>category:</b>" & Server.HTMLEncode(category) & "<br>"
Next

Function GetText(context, xpath)
    Dim node
    GetText = ""
    If Not context Is Nothing And xpath > "" Then
        Set node = context.selectSingleNode(xpath)
        If Not node Is Nothing Then GetText = node.text
    End If
End Function