bctje Posted August 27, 2010 Share Posted August 27, 2010 HelloI'm looking for some help to develop a script as follows:Within an xml file I want to replace the text between the <PROPERTY NAME="SD SERVER"> and </PROPERTY> tags with another value.The text between the tags can be anything, so it is not always equal to serverx.The xml file looks like this:<?xml version="1.0" encoding="UTF-8"?><!--Copyright © 2004 Company, L.P. All Rights Reserved.--><SETTINGS><PROPERTY NAME="PRODUCTDATADIR">D:\applicationpath\</PROPERTY><PROPERTY NAME="PRODUCTINSTALLDIR">C:\installpath\</PROPERTY><GROUP NAME="ACCOUNTS"><PROPERTY NAME="DEFAULT">1</PROPERTY><GROUP NAME="1"><PROPERTY NAME="ACCOUNT ID">0</PROPERTY><PROPERTY NAME="ACCOUNT NAME">user.name</PROPERTY><PROPERTY NAME="ACCOUNT TYPE">2</PROPERTY><PROPERTY NAME="ACCOUNT VERSION">0</PROPERTY><PROPERTY NAME="SD ACCOUNT NAME">user.name</PROPERTY><PROPERTY NAME="SD PASSWORD">whatever</PROPERTY><PROPERTY NAME="SD SERVER">serverx</PROPERTY><PROPERTY NAME="USEONLYTHISSERVER">false</PROPERTY></GROUP></GROUP></SETTINGS>Thanks! Link to comment Share on other sites More sharing options...
wakillon Posted August 27, 2010 Share Posted August 27, 2010 (edited) #include <String.au3> #include <File.au3> Dim $_String, $_StringYouWant='whatYouWant', $_XmlFilePath = '' ; path of your Xml File $file = FileOpen ( $_XmlFilePath ) $_String = FileRead ( $file ) $_StringBetween = _StringBetween ( $_String, '<PROPERTY NAME="SD SERVER">', '</PROPERTY>' ) If Not @error Then ConsoleWrite ( "Result : " & $_StringBetween[0] & @Crlf ) $_String = StringReplace ( $_String, $_StringBetween[0], $_StringYouWant ) EndIf FileWrite ( $file, $_String ) FileClose ( $file ) Edited August 27, 2010 by wakillon AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 27, 2010 Share Posted August 27, 2010 If you want to work with the XML directly: #include <Array.au3> #include <_XMLDOMWrapper.au3> $debugging = True ; Open XML file $sXML = @ScriptDir & "\Test1.xml" $iRET = _XMLFileOpen($sXML) ConsoleWrite("Debug: $iRET = " & $iRET & @LF) ; Display old value $aValue = _XMLGetValue("/SETTINGS/GROUP[@NAME='ACCOUNTS']/GROUP[@NAME='1']/PROPERTY[@NAME='SD SERVER']") _ArrayDisplay($aValue, "$aValue") ; Set new value _XMLUpdateField("/SETTINGS/GROUP[@NAME='ACCOUNTS']/GROUP[@NAME='1']/PROPERTY[@NAME='SD SERVER']", "My New Value Goes Here") ; Display New value $aValue = _XMLGetValue("/SETTINGS/GROUP[@NAME='ACCOUNTS']/GROUP[@NAME='1']/PROPERTY[@NAME='SD SERVER']") _ArrayDisplay($aValue, "$aValue") Note the syntax of the XPath used. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
bctje Posted August 27, 2010 Author Share Posted August 27, 2010 Many thanks for the replies...but I can't get it work. @PsaltyDS I get an error opening the file #include <_XMLDOMWrapper.au3>, because I think I don't have it on my computer. Where can I get it? @wakillon Nothing seems to happen I still need to analyse the scripts, I guess Link to comment Share on other sites More sharing options...
wakillon Posted August 27, 2010 Share Posted August 27, 2010 Many thanks for the replies...but I can't get it work.@wakillonNothing seems to happenI still need to analyse the scripts, I guess Did you write your xml file path after $_XmlFilePath = It should work and your file should be modified ! AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
bctje Posted August 27, 2010 Author Share Posted August 27, 2010 (edited) Yes, I did. I debugged the script, so I knows the file has been opened. I also copied String.au3 and File.au3 to the same folder as the script. So far so good, but nothing happens after opening the xml file...it is not updated at all. edit: I feel so Newbie Edited August 27, 2010 by bctje Link to comment Share on other sites More sharing options...
wakillon Posted August 27, 2010 Share Posted August 27, 2010 Yes, I did. I debugged the script, so I knows the file has been opened. I also copied String.au3 and File.au3 to the same folder as the script. So far so good, but nothing happens after opening the xml file...it is not updated at all. I forget to open file in write mode ! #include <String.au3> #include <File.au3> Dim $_String, $_StringYouWant='whatYouWant', $_XmlFilePath = '' ; path of your Xml File $file = FileOpen ( $_XmlFilePath ) $_String = FileRead ( $file ) $_StringBetween = _StringBetween ( $_String, '<PROPERTY NAME="SD SERVER">', '</PROPERTY>' ) If Not @error Then ConsoleWrite ( "Result : " & $_StringBetween[0] & @Crlf ) $_String = StringReplace ( $_String, $_StringBetween[0], $_StringYouWant ) EndIf FileClose ( $file ) $file = FileOpen ( $_XmlFilePath, 2 ) FileWrite ( $file, $_String ) FileClose ( $file ) AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
bctje Posted August 27, 2010 Author Share Posted August 27, 2010 I knew you were the problem...ok, it really works fine! Thanks a lot for the instant online support Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 27, 2010 Share Posted August 27, 2010 Many thanks for the replies...but I can't get it work. @PsaltyDS I get an error opening the file #include <_XMLDOMWrapper.au3>, because I think I don't have it on my computer. Where can I get it? It's a UDF posted in Example Scripts by eltorro: XML DOM wrapper (COM) Note that I also had to remove the "©" symbol from the comment line because the UTF-8 encoding didn't like it. This was what I used to test: <?xml version="1.0" encoding="UTF-8"?> <!--Copyright 2004 Company, L.P. All Rights Reserved.--> <SETTINGS> <PROPERTY NAME="PRODUCTDATADIR">D:\applicationpath\</PROPERTY> <PROPERTY NAME="PRODUCTINSTALLDIR">C:\installpath\</PROPERTY> <GROUP NAME="ACCOUNTS"> <PROPERTY NAME="DEFAULT">1</PROPERTY> <GROUP NAME="1"> <PROPERTY NAME="ACCOUNT ID">0</PROPERTY> <PROPERTY NAME="ACCOUNT NAME">user.name</PROPERTY> <PROPERTY NAME="ACCOUNT TYPE">2</PROPERTY> <PROPERTY NAME="ACCOUNT VERSION">0</PROPERTY> <PROPERTY NAME="SD ACCOUNT NAME">user.name</PROPERTY> <PROPERTY NAME="SD PASSWORD">whatever</PROPERTY> <PROPERTY NAME="SD SERVER">My New Value Goes Here</PROPERTY> <PROPERTY NAME="USEONLYTHISSERVER">false</PROPERTY> </GROUP> </GROUP> </SETTINGS> Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
wakillon Posted August 27, 2010 Share Posted August 27, 2010 (edited) I knew you were the problem...ok, it really works fine!Thanks a lot for the instant online support Sorry for the lost time but I begin also ! Link for _XMLDomWrapper.au3 Edited August 27, 2010 by wakillon AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
bctje Posted August 27, 2010 Author Share Posted August 27, 2010 Pretty, cool, I downloaded _XMLDomWrapper.au3 and now both scripts are working now! Thanks a lot, topic closed and 10 points for each of you! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now