Sign in to follow this  
Followers 0
bctje

Replace text in file between known tags

11 posts in this topic

Hello

I'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!

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Many thanks for the replies...but I can't get it work.

@wakillon

Nothing seems to happen

I 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 ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by bctje

Share this post


Link to post
Share on other sites

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 ! Posted Image

#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.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

I knew you were the problem...ok, it really works fine!

Thanks a lot for the instant online support ;)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 ! Posted Image

Link for _XMLDomWrapper.au3

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0