How to get XML attribute value?

心已入冬 提交于 2019-12-02 14:54:30

问题


Set XmlDocument = Server.CreateObject("Msxml2.DOMDocument.3.0")
XmlDocument.SetProperty "ServerHTTPRequest", True
XmlDocument.Async = False
XmlDocument.Load("books.xml")
Set Items = XmlDocument.selectNodes("//book")
For Each Item in Items
    Set title = Item.SelectSingleNode("title/text()")
    S_title = Trim(title.data)

    Set price = Item.SelectSingleNode("price/text()")
    S_price = Trim(price.data)
    response.Write S_title & S_price 

    Set Objavail = price.GetAttribute("avail")
    S_avail = Objavail.value
    response.Write S_avail &"<br>"
Next

It seem to me identical like the method described here, but I can not read attribute value by this way. S_title and S_price values are OK.

Error info:

Object doesn't support this property or method: 'getAttribute'.

Here is my books.xml:

<?xml version="1.0"?>
<bookstore>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price avail="yes">30.00</price>
    </book>
    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price avail="no">29.99</price>
    </book>
</bookstore>

回答1:


SelectSingleNode("price/text()") selects the text node (the "body" if you will) inside the <price> tag. The nested text node doesn't have an attribute avail. Also, GetAttribute() doesn't return an object, so you must not use the Set keyword there.

Change this:

Set title = Item.SelectSingleNode("title/text()")
S_title = Trim(title.data)

Set price = Item.SelectSingleNode("price/text()")
S_price = Trim(price.data)  
response.Write S_title & S_price 

Set Objavail = PRICE.GetAttribute("avail")
S_avail = Objavail.value
response.Write S_avail &"<br>"

into this:

Set title = Item.SelectSingleNode("title")
S_title = Trim(title.text)

Set price = Item.SelectSingleNode("price")
S_price = Trim(price.text)
response.Write S_title & S_price 

S_avail = price.GetAttribute("avail")
response.Write S_avail &"<br>"


来源:https://stackoverflow.com/questions/41535691/how-to-get-xml-attribute-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!