[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

read XML file VB script

Posted on 2012-04-05
17
Medium Priority
?
1,082 Views
Last Modified: 2012-04-17
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
Comment
Question by:Murali
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 Comments
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37810425
Here is a sample .
3.zip
0
 
LVL 10

Author Comment

by:Murali
ID: 37810584
need vbs file... dont have vb6
0
 
LVL 19

Accepted Solution

by:
zc2 earned 1900 total points
ID: 37811804
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Author Comment

by:Murali
ID: 37815412
Hi ZC2, thanks for your assitance.. am going throught script the checking.. will update you soon...
0
 
LVL 10

Author Comment

by:Murali
ID: 37822504
Hi ZC2, it shows error
Object required: 'documentElement'
0
 
LVL 10

Author Comment

by:Murali
ID: 37822597
I have changed the input xml file and xml version is 1, encoding UTF 8
0
 
LVL 10

Author Comment

by:Murali
ID: 37822890
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
 
LVL 19

Expert Comment

by:zc2
ID: 37823489
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
 
LVL 10

Author Comment

by:Murali
ID: 37827242
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
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 100 total points
ID: 37827280
NOT FOR POINTS

Are you saving after the edit:


xml.save xml.url

Chris
0
 
LVL 10

Author Comment

by:Murali
ID: 37827382
it is showing some msg

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

selected node /shop/... and displaying the nm value i have defined...
0
 
LVL 10

Author Comment

by:Murali
ID: 37827390
for adding new line working perfect :)
0
 
LVL 19

Expert Comment

by:zc2
ID: 37827861
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
 
LVL 10

Author Closing Comment

by:Murali
ID: 37827934
ZC2, EE does not allowed me to give more than 500!!! otherwise anything for you.. Loved it.. thank alot
0
 
LVL 19

Expert Comment

by:zc2
ID: 37827950
you're welcome
0
 
LVL 10

Author Comment

by:Murali
ID: 37854488
Hi AZ2, in few servers it is showing following error.

activex component can't create object: 'Msxml2.domdocument.6.0'
0
 
LVL 19

Expert Comment

by:zc2
ID: 37856121
Please make sure the MSXML v6 is installed, or try to change the PROGID string from "Msxml2.DOMDocument.6.0" to "Msxml2.DOMDocument"
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question