read XML file VB script

Hi, I have xml file something like this

<shop>
      
      <fruit="apple" cost="20" />
      <fruit="orange" cost="30" />
      <fruit="mango" cost="50" />
      <vegetable="tamoto" cost="10" />
      <vegetable="potato" cost="15" />
      <fruit="curly flower" cost="60" />

</shop>

How do I execte the following operation in vbscript
read all furits and vegetable
Modify the name and cost
delete any line
add some more line

Thanks
LVL 10
MuraliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vb_elmarCommented:
Here is a sample .
3.zip
0
MuraliAuthor Commented:
need vbs file... dont have vb6
0
zc2Commented:
First of all, your XML is not well formed.
If it exactly the data format you're dealing with, you should not call it XML and treat it just as complex text structure.

Here a modified version of the data XML which is well-formed:

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

Open in new window


Here's the sample vbscript:

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

MuraliAuthor Commented:
Hi ZC2, thanks for your assitance.. am going throught script the checking.. will update you soon...
0
MuraliAuthor Commented:
Hi ZC2, it shows error
Object required: 'documentElement'
0
MuraliAuthor Commented:
I have changed the input xml file and xml version is 1, encoding UTF 8
0
MuraliAuthor Commented:
I have modifed the script variables to another xml file... That XML file has a dtd file defined in it...

When I run the vbs script file it is showing error DTD phibited and documentElement not found...

If I remove the the line which load the DTD file then the script works fine..

Could you please assist.
0
zc2Commented:
you could try to add the following lines after the line 4.

xml.validateonparse=false
xml.setProperty "ProhibitDTD", false

In my example the line 9 prints out the error but actually does not terminate the script. Please modify the script the way you want so it will correctly terminated or do anything you need when an error occurs.
0
MuraliAuthor Commented:
Hi ZC2, thanks that worked.. now trying to delete the line.. I tried the example written here exactly... tried to delete the mango...

The scripts executes with no error but nothing happens.. I still can see the mango in xml file.. does not deletes...

I save the above correct xml to shop.xml file... and this what I used....

Option Explicit
Dim xml
set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.validateonparse=false
xml.setProperty "ProhibitDTD", false


' 1. read the XML
xml.load("shop.xml")
if xml.parseError.errorCode <> 0 then
   WScript.Echo "Error: " & xml.parseError.errorCode & " Reason: " & xml.parseError.reason
end if
call xml.setProperty("SelectionLanguage", "XPath")


' 4. Delete "mango"
dim e, nm
nm = "mango"
set e = xml.documentElement.selectSingleNode("/shop/*[@name='" & nm & "']")
if not e is Nothing then e.parentNode.removeChild( e )
0
Chris BottomleySoftware Quality Lead EngineerCommented:
NOT FOR POINTS

Are you saving after the edit:


xml.save xml.url

Chris
0
MuraliAuthor Commented:
it is showing some msg

msxml6.dll: Expected token 'EOF' found '*'.

selected node /shop/... and displaying the nm value i have defined...
0
MuraliAuthor Commented:
for adding new line working perfect :)
0
zc2Commented:
I ran your shorten version of the script, it deletes the mango node seamlessly.
Sorry, for a dull question, do you realize you need to print out or save the xml file? The script does not modify the original shop.xml, it works with the node tree in the memory.
0
MuraliAuthor Commented:
ZC2, EE does not allowed me to give more than 500!!! otherwise anything for you.. Loved it.. thank alot
0
zc2Commented:
you're welcome
0
MuraliAuthor Commented:
Hi AZ2, in few servers it is showing following error.

activex component can't create object: 'Msxml2.domdocument.6.0'
0
zc2Commented:
Please make sure the MSXML v6 is installed, or try to change the PROGID string from "Msxml2.DOMDocument.6.0" to "Msxml2.DOMDocument"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.