Solved

read XML file VB script

Posted on 2012-04-05
17
1,023 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
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 18

Accepted Solution

by:
zc2 earned 475 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
 
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 18

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

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

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 18

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 18

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now