Jump to content

Replace text within .xml


Rogue5099
 Share

Recommended Posts

I want to change <Value> from true to false in a .xml document but I can't get it to save??

<?xml version="1.0" encoding="utf-8"?>
<Settings>
  <Section1>
    <Section2>
      <Value>true</Value>
    </Section2>
  </Section1>
</Settings>

I can read it and replace string but it won't save? If there is an easier way please let me know!

Link to comment
Share on other sites

Are you writing to the same file? Without any code I would say that it is a permission issue or handle (locked file) issue. Are you using FileOpen/FileClose? If so, are you using them correctly? Post the relevant code.

Edited by Varian
Link to comment
Share on other sites

This is a sample from the code. Once done it will handle serveral lines hince the complexity.

#include <File.au3>
#include <Array.au3>
Global $LINES
$sXmlFile = "C:\Test.xml"
FileOpen($sXmlFile, 0)

If _FILEREADTOARRAY($sXmlFile, $LINES) = 1 Then
        For $I = 1 To $LINES[0]
            If $LINES[$I] <> "" Then
                If StringInStr($LINES[$I], "<Value>true</Value>", 0) <> 0 Then 
                          StringReplace($LINES[$I], "true", "false")
                EndIf
            EndIf
        Next
    EndIf
    
FileClose($sXmlFile)

Not exactly what I wanted but this works:

#include <File.au3>
#include <Array.au3>
Global $LINES
$sXmlFile = "C:\Test.xml"

If _FILEREADTOARRAY($sXmlFile, $LINES) = 1 Then
        For $I = 1 To $LINES[0]
            If $LINES[$I] <> "" Then
                If StringInStr($LINES[$I], "<Value>true</Value>", 0) <> 0 Then 
                    _FileWriteToLine($sXmlFile, $I, "<Value>false</Value>", 1)
                EndIf
            EndIf
        Next
EndIf
Edited by rogue5099
Link to comment
Share on other sites

Learn XPath syntax and read/write to it directly:

#include <_XMLDOMWrapper.au3>
#include <Array.au3> ; Only for _ArrayDisplay()

$sXmlFile = @ScriptDir & "\Test1.xml"
$sXPath = "/Settings/Section1/Section2/Value"

_XMLFileOpen($sXmlFile)

For $n = 1 To 4
    $aRET = _XMLGetValue($sXPath)
    _ArrayDisplay($aRET, "Before")

    $sNewVal = String($aRET[1] = "False")
    _XMLUpdateField($sXPath, $sNewVal)
    $aRET = _XMLGetValue($sXPath)
    _ArrayDisplay($aRET, "After")
Next

;)

Edited by PsaltyDS
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

Both StringReplace() and _ReplaceStringInFile() are going to become unwieldy and dangerous as the XML involved gets more complicated. What if you only wanted to change that value on the third of twenty selections in the file? Then change only the 12th one? Very easy to do with XPath, a real headache trying to parse the whole file with your own string manipulation.

;)

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

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...