如何获取XML属性值?

时间:2017-01-08 17:38:40

标签: xml vbscript msxml

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

在我看来,与here描述的方法完全相同,但我无法通过这种方式读取属性值。 S_titleS_price值都可以。

错误信息:

  

对象不支持此属性或方法:&#39; getAttribute&#39;。

这是我的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 个答案:

答案 0 :(得分:2)

SelectSingleNode("price/text()")<price>标记内选择文本节点(如果您愿意,则选择“正文”)。嵌套文本节点没有属性avail。另外,GetAttribute()不会返回对象,因此您不能在那里使用Set关键字。

改变这个:

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

进入这个:

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