【问题标题】:Using getNamedItem function in DOM在 DOM 中使用 getNamedItem 函数
【发布时间】:2013-02-19 20:11:01
【问题描述】:

我试图通过 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>

【问题讨论】:

  • 这里是 xml 日常意大利语 Giada De Laurentiis200530.00哈利波特 J K. Rowling200529.99

标签: xml dom vbscript


【解决方案1】:

错误的引用:

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

【讨论】:

  • 谢谢,一个问题 IXMLDOMNode 是什么意思?
  • @user1925406 - 'IXMLDOMNode' 是getNamedItem() 返回的对象的类/类型名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 2020-01-18
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
相关资源
最近更新 更多