The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!
<shop>
<fruit name="apple" cost="20" />
<fruit name="orange" cost="30" />
<fruit name="mango" cost="50" />
<vegetable name="tamoto" cost="10" />
<vegetable name="potato" cost="15" />
<fruit name="curly flower" cost="60" />
</shop>
Option Explicit
Dim xml
set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
' 1. read the XML
xml.load("fruit.xml")
if xml.parseError.errorCode <> 0 then
WScript.Echo "Error: " & xml.parseError.errorCode & " Reason: " & xml.parseError.reason
end if
call xml.setProperty("SelectionLanguage", "XPath")
' 2. List of the all fruits and vegetables
dim f_ee, f_e
set f_ee = xml.documentElement.selectNodes("/shop/fruit|vegetable")
WScript.Echo "Found: " & f_ee.length
For each f_e in f_ee
WScript.Echo "Name:" & f_e.getAttribute("name")
Next
' 3. Modify the names and costs
dim na, ca
For each f_e in f_ee
set na = f_e.getAttributeNode("name")
na.nodeValue = "Good " & na.nodeValue
set ca = f_e.getAttributeNode("cost")
ca.nodeValue = CDbl( ca.nodeValue ) + 10.0
Next
' 4. Delete "mango"
dim e, nm
nm = "Good mango"
set e = xml.documentElement.selectSingleNode("/shop/*[@name='" & nm & "']")
if not e is Nothing then e.parentNode.removeChild( e )
' 5. Add the pineapple fruit
dim shop
set shop = xml.documentElement.selectSingleNode("/shop")
set e = xml.createElement( "fruit" )
call e.setAttribute( "name", "pineapple" )
call e.setAttribute( "cost", "88" )
shop.appendChild( e )
' 6. Printing out the result
WScript.Echo xml.xml
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
3.zip