Link to home
Start Free TrialLog in
Avatar of Murali
MuraliFlag for India

asked on

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
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Here is a sample .
3.zip
Avatar of Murali

ASKER

need vbs file... dont have vb6
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murali

ASKER

Hi ZC2, thanks for your assitance.. am going throught script the checking.. will update you soon...
Avatar of Murali

ASKER

Hi ZC2, it shows error
Object required: 'documentElement'
Avatar of Murali

ASKER

I have changed the input xml file and xml version is 1, encoding UTF 8
Avatar of Murali

ASKER

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.
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.
Avatar of Murali

ASKER

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 )
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murali

ASKER

it is showing some msg

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

selected node /shop/... and displaying the nm value i have defined...
Avatar of Murali

ASKER

for adding new line working perfect :)
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.
Avatar of Murali

ASKER

ZC2, EE does not allowed me to give more than 500!!! otherwise anything for you.. Loved it.. thank alot
you're welcome
Avatar of Murali

ASKER

Hi AZ2, in few servers it is showing following error.

activex component can't create object: 'Msxml2.domdocument.6.0'
Please make sure the MSXML v6 is installed, or try to change the PROGID string from "Msxml2.DOMDocument.6.0" to "Msxml2.DOMDocument"