Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the best way to modify an XML file? 1

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
I have an XML file that I want to modify. For the most part, I just need to change a line like this:
Code:
<Company:Qty>-20</Company:Qty>
to a line like this:
Code:
<Company:Qty>20</Company:Qty>
(basically, just removing the dash)

My first thought was to treat it like a regular text file and read the entire file into a string, modify the string (using a regular expression find/replace), and then save that string to a new file. I have a feeling that there's a better way to do this via the XMLDOM but I can't figure it out. Is there a good online reference for all of the methods available within VBScript for manipulating XML documents?

I'd appreciate it if someone could give me a nudge in the right direction.
 
Search the forum or google for "VBScript XML DOM"

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks for the suggestion EBGreen, but I tried that already. I wouldn't be asking here if that had worked to my satisfaction.
 
You can get xpath to all Company:Qty and verify if it starts with a negative sign (your "dash"). If yes, eliminate it. Something like this.
[tt]
[green]xmlfile="d:\test\input.xml" 'your source file name
outfile="d:\test\output.xml" 'your output file name-maybe the same
snsuri="xmlns:Company='urn:test-company'" 'your namespace[/green]

set oparser=createobject("msxml2.domdocument")
with oparser
.async=false
.validateOnParse=false
.resolveExternals=false
.load xmlfile
end with

if oparser.parseerror.errorcode<>0 then
wscript.echo "xml file " & xmlfile & " is not well-formed." & vbcrlf & "Operation aborted."
wscript.quit 999
end if

with oparser
.setproperty "SelectionLanguage","XPath"
.setproperty "SelectionNamespaces",[green]snsuri 'your namespace uri[/green]
end with

set onodelist=oparser.documentelement.selectnodes("//Company:Qty")
for i=0 to onodelist.length-1
s=trim(onodelist.item(i).text)
if len(s)>0 then
if left(s,1)="-" then
s=right(s,len(s)-1)
end if
end if
onodelist.item(i).text=s
next

oparser.save outfile

set onodelist=nothing
set oparser=nothing
[/tt]
 
Thanks tsuji. Once again, you've solved my problem.

I also noticed something interesting. Your script makes the resulting XML code look nicer due to a uniform pattern of indenting the lines and removal of blank lines. Is this a built-in function of the parser? In case my boss asks, is there a way to control this? He may want to preserve the way the code looked originally, if possible.
 
You may set this attribute.
[tt]
with oparser
.async=false
.validateOnParse=false
.resolveExternals=false
[blue].preservewhitespace=true[/blue]
.load xmlfile
end with
[/tt]
 
Thanks for the help tsuji. My script appears to be running perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top