• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1104
  • Last Modified:

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
0
Murali
Asked:
Murali
2 Solutions
 
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
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 BottomleyCommented:
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
Question has a verified solution.

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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now