Jump to content
Sign in to follow this  
pintas

(SOLVED!) _StringBetween problem in XML... Help please!

Recommended Posts

pintas

I'm trying to edit this XML:

<?xml version="1.0" encoding="utf-8"?>
<preferences>
    <section id="106785699278404793101245697028">
    <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>
  </section>
  <section id="568005212148609391421245697028">
    <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>
  </section>
  <section id="689115594670726692631245697028">
    <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>
  </section>
</preferences>

And i want to replace the paths 'C:\Doc.doc' with '@ScriptDir & "\App\unite\fileSharing.us"' between <value...> and </value>, but the section ids always differ and the xml gets bigger everytime i run the program. How can i replace all the paths?

I can only change the first path...

$sNewData = @ScriptDir & "\App\unite\fileSharing.us"
$sXMLFile = @ScriptDir & "\App\profile\widgets\widgets.dat"

$sXML = FileRead($sXMLFile)
$avData = _StringBetween($sXML, '<value id="path to widget data" xml:space="preserve">', '</value>')
$sXML = StringReplace($sXML, '<value id="path to widget data" xml:space="preserve">' & $avData[0] & '</value>', '<value id="path to widget data" xml:space="preserve">' & $sNewData & '</value>')
$hXMLFile = FileOpen($sXMLFile, 2) ; 2 = Overwrite
FileWrite($hXMLFile, $sXML)

Help please?

:D

Edited by pintas

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

why search for the part before and behind? Couldn't you just use this pattern?

>(\w.+?)</value>

with the function StringRegReplace ?

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
pintas

But wouldn't the problem remain that way?

I couldn't find any help in the help file, could you post a small example please?

Oh, and btw, the path C:\Doc.doc is a variable... :S

I just want to replace all the strings between <value...> and </value> with the same string (@ScriptDir & "\App\unite\fileSharing.us")

Thanks for your reply

Edited by pintas

Share this post


Link to post
Share on other sites
Xenobiologist

Global $newStr = StringReplace(@ScriptDir & "\App\unite\fileSharing.us", '\', '\\')
Global $str = '<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
        '<preferences>' & @CRLF & _
        '    <section id="106785699278404793101245697028">' & @CRLF & _
        '    <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>' & @CRLF & _
        '  </section>' & @CRLF & _
        '  <section id="568005212148609391421245697028">' & @CRLF & _
        '    <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>' & @CRLF & _
        '  </section>' & @CRLF & _
        '  <section id="689115594670726692631245697028">' & @CRLF & _
        '    <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>' & @CRLF & _
        '  </section>' & @CRLF & _
        '</preferences>'

ConsoleWrite(StringRegExpReplace($str, '">(\w.+?)</value>', $newStr) & @CRLF)


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
pintas

Thank you. That is a big help but as i said in the first post, the xml grows everytime i launch the program so, there will be more than three replacements. There can be hundreds, and that's where my problem lies...

How do i change all the document? Anyway to use a 'For' or someway to find and replace?

Thanks again.

Share this post


Link to post
Share on other sites
99ojo

Thank you. That is a big help but as i said in the first post, the xml grows everytime i launch the program so, there will be more than three replacements. There can be hundreds, and that's where my problem lies...

How do i change all the document? Anyway to use a 'For' or someway to find and replace?

Thanks again.

Hi,

doesn't a simple _ReplaceStringInFile the job you want?

;-))

Stefan

Share this post


Link to post
Share on other sites
pintas

LOL! You know... it probably does it :D Why do we humans tend to complicate so much? ;)

Thanks man. I'll give it a try and come right back to post the (successfull) results. :D

Edited by pintas

Share this post


Link to post
Share on other sites
SmOke_N

Thank you. That is a big help but as i said in the first post, the xml grows everytime i launch the program so, there will be more than three replacements. There can be hundreds, and that's where my problem lies...

How do i change all the document? Anyway to use a 'For' or someway to find and replace?

Thanks again.

Global $sNewData = StringReplace(@ScriptDir & "\App\unite\fileSharing.us", "\", "\\") ; Have to escape back slashes for regexreplace
Global $sXMLFile = @ScriptDir & "\App\profile\widgets\widgets.dat"

Global $sXML = FileRead($sXMLFile)
Global $sStart = '<value id="path to widget data" xml:space="preserve">'
Global $sEnd = "</value>"
$sXML = StringRegExpReplace($sXML, "(?i)(" & $sStart & ")(.*?)(" & $sEnd & ")", "$1" & $sNewData & "$3") ; Will replace all matches in file
FileClose(FileOpen($sXMLFile, 2))
FileWrite($sXMLFile, $sXML)

Edit:

Didn't even read xeno's! ... his is pretty much the same as mine, just a different approach.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
pintas

Done it! :D

$path = @ScriptDir & "\App"
$filename = @ScriptDir & "\App\profile\widgets\widgets.dat"
$find = IniRead(@ScriptDir & '\Data\pathupdate.ini', "PathUpdate", "Old","")
$replace = $path
FileWrite($filename, $path)
$retval = _ReplaceStringInFile($filename,$find,$replace)

Thanks guys for your help. This one is solved! :D

Edited by pintas

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  

×