0

我试图通过 Vb 脚本在 xml DOM 中使用函数 getNamedItem,但它永远不会工作。下面是一段代码和 XML 文档。请帮助我找出错误,在此先感谢。

Set obj = CreateObject("Microsoft.xmldom")
obj.async = false
obj.load ("C:\Users\ACER\Desktop\Project Folder\Parsing XML\Books.xml")
Set root = obj.documentElement
Set child = root.childNodes
Set Node = child.item(1)
s=Node.getNamedItem('author')
Msgbox s

XML 文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>
4

1 回答 1

0

错误的引用:

getNamedItem('author') - use double quotes "

错误的功能:

getNamedItem - Returns IXMLDOMNode object for the specified attribute.

就像是:

 Dim root  : Set root  = objMSXML.documentElement
 Dim child : Set child = root.childNodes
 Dim Node  : Set Node = child.item(1)
 Dim ndFnd : Set ndFnd = Node.selectSingleNode("author")
 WScript.Echo ndFnd.text

会给你:

J K. Rowling

getNamedItem() 的示例用法:

...     
Dim Node  : Set Node = child.item(1)
WScript.Echo Node.attributes.getNamedItem("category").value
于 2013-02-19T20:29:53.843 回答