Using getNamedItem function in DOM

2019-09-08 05:19发布

Am trying to use the function getNamedItem in xml DOM through Vb script but it never works. Below is the piece of code and XMl document. Please help me in finding out the error, thanks in advance.

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 file:

<?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 dom vbscript
1条回答
淡お忘
2楼-- · 2019-09-08 05:52

Wrong quotes:

getNamedItem('author') - use double quotes "

Wrong function:

getNamedItem - Returns IXMLDOMNode object for the specified attribute.

Something like:

 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

will give you:

J K. Rowling

Sample use of getNamedItem():

...     
Dim Node  : Set Node = child.item(1)
WScript.Echo Node.attributes.getNamedItem("category").value
查看更多
登录 后发表回答