Changing a value in a xml file

May 28, 2009 at 12:18:44
Specs: Windows Server 2003
I am trying to change a value in an xml file. The script asks for the new value. The value in the xml
file could be any number (what ever the previous version is) and could be on any line in the file. The only thing I can count on is the content of the line that the value is located.
Here is a small part of the XML file and what I want to change..
------------------------------------------------------------------------
johntest.xml

<DatabaseTables>
<Database name="ParadigmPlusCatalogConfig" path="\Configurators\CP\CP1\Database\ParadigmPlusCatalogConfig.mdb"

version="24">
<Table name="Items" version="24" /> <---- I WANT TO CHANGE THIS VALUE FROM 24 (in this case) TO WHATEVER
</Database>
</DatabaseTables>
<BrandedDatabaseTables />
<ZipFiles>

----------------------------------------------------------------------------

I want to change the value of <Table name="Items" version="24" /> to a different version number.
Here is what I have tried so far.
'--------------------------------------
Const ForReading = 1
Const ForWriting = 2

strNew = InputBox("Please enter NEW Catalog Version Number")

If strNew = "" Then
Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\dir1\dir2\johntest.xml", ForReading)
strText = objFile.ReadAll

Do Until objFile.AtEndOfStream
strOld = ""
strSearchString = objFile.ReadLine

intStart = InStr(strSearchString, "" & chr(34) & "Items" & chr(34) & " version=" & chr(34) & "")

If intStart <> 0 Then
intStart = intStart + 17
strText = Mid(strSearchString, intStart, 250)

For i = 1 to Len(strText)
If Mid(strText, i, 1) = "" & chr(34) & "" Then
wscript.Echo strOld
Exit For
Else
strOld = strOld & Mid(strText, i, 1)
End If
Next
End If

Loop
objFile.Close

strNewText = Replace(strText, "" & chr(34) & "Items" & chr(34) & " version=" & chr(34) & "" & strOld , "" & chr(34) &

"Items" & chr(34) & " version=" & chr(34) & "" & strNew )

Set objFile = objFSO.OpenTextFile("E:\dir1\dir2\johntest.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Close

'-----------------------------------------------------------

When I run this the vbs file it asks for the new version number then finds the line with "Items" version=". Using that value I try to replace the lines.
The problem is when it writes back to the xml file, it prefixes to the value and does not replace it. For example, the old value of the version is "24" - I run the script and input 55. The result on the line ends up as "5524".
Any help on this would be appreciated.

JK


See More: Changing a value in a xml file

Report •


#1
June 4, 2009 at 13:53:31
I found what I was doing wrong and thought a follow up may be useful.
Set objFile = objFSO.OpenTextFile("E:\dir1\dir2\johntest.xml", ForReading)
strText = objFile.ReadAll

Do Until objFile.AtEndOfStream <<-- Not right - I was closing the file then doing a do until endofstream.
Here is the solution I came up with.

Const ForReading = 1
Const ForWriting = 2
dim strNew, strOld, strText, strText2, strSearchString, strNewText

strNew = InputBox("Please enter NEW Catalog Version Number")
If strNew = "" Then
Wscript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\dir1\dir2\johntest.xml", ForReading)
strText = objFile.ReadAll
objFile.Close
'-------------------------------- Start with First Line ---------------------
strOld = ""
strText2 = strText


intStart = InStr(strText, "ParadigmPlusCatalogConfig.mdb" & chr(34) & " version=" & chr(34) & "")
'wscript.echo intStart

If intStart <> 0 Then
intStart = intStart + 40
strText2 = Mid(strText2, intStart, 250)
'wscript.echo strText2

For i = 1 to Len(strText2)
If Mid(strText2, i, 1) = "" & chr(34) & "" Then
Exit For
Else
strOld = strOld & Mid(strText2, i, 1)
'wscript.echo strOld
End If
Next
End If

'wscript.echo strOld
strText = Replace(strText, ("ParadigmPlusCatalogConfig.mdb" & chr(34) & " version=" & chr(34) & "" & strOld) , ("ParadigmPlusCatalogConfig.mdb" & chr(34) & " version=" & chr(34) & "" & strNew) )
'-------------------------------------- Write to File --------------------
'wscript.echo strOld
strNewText = Replace(strText, ("" & chr(34) & "Items" & chr(34) & " version=" & chr(34) & "" & strOld) , ("" & chr(34) & "Items" & chr(34) & " version=" & chr(34) & "" & strNew) )

Set objFile = objFSO.OpenTextFile("E:\dir1\dir2\johntest.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Close


Hope this helps someone some time.
JK


Report •
Related Solutions


Ask Question