Solved

read XML file VB script

Posted on 2012-04-05
17
1,061 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 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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

695 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