Jump to content

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


Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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.

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...