Jump to content

Search and Replace strings in a multiple XML


Recommended Posts

Hello AutoITs MVPs,

I'm not quiet good enough in autoit scripting that's why I'm posting here and asking for a help.

Can somebody help me on how to search for string and replace it after in a multiple XMLs. See below for an xml element:

<Eq ID="DE1"><EqSource Format="TEX"><![CDATA[ $$\begin{array}{*{20}l} Kw+Pa &= V,\\ P^Tw &= 0, \end{array} $$ ]]></EqSource></Eq>

Where the highlighted in red should be replace to the highlighted text in green:

<EqSource Format="TEX"> = <Source Format="LATEX">

</EqSource> = </Source>

My head is aching as there were a lot xmls and pretty sure its about 10. Any help is much appreciated.

Thanks and regards,

Ray

Link to comment
Share on other sites

Not sure I really understand, but have you looked at the function StringReplace()? Maybe this will get you started?

$string = '<Eq ID="DE1"><EqSource Format="TEX"><![CDATA[ $$\begin{array}{*{20}l} Kw+Pa &= V,\\ P^Tw &= 0, \end{array} $$ ]]></EqSource></Eq>'
$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'
StringReplace ( $string, $StringSearch, $StringReplace)
Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Try this:

$String = '<Eq ID="DE1"><EqSource Format="TEX"><![CDATA[ $$\begin{array}{*{20}l} Kw+Pa &= V,\\ P^Tw &= 0, \end{array} $$ ]]></EqSource></Eq>'

$String = StringReplace($String, '<EqSource Format="TEX">','<Source Format="LATEX">')
$String = StringReplace($String, '</EqSource>', "</Source>")

MsgBox(0, "New String", "Newstring is: " & $String)

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

Thanks nitekram! How about to list all xmls in the file list?

#include <File.au3>
$aFileList = _FileListToArray("D:WorkDir\DataSources\Output\", "*.xml", 1)
$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'
StringReplace ($aFileList, $StringSearch, $StringReplace)

It seems not working

_FileListToArray return an array and you are tring to use StringReplace with an array, this will never work ;)

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

Thanks but how can I do that? Any help Prodigy?

I'm Nessie ;)

With _FileListToArray you will only "see" the name of all the .xml file contained in the folder. Can you please explain step by step what you want to do?

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

Not tested, but might get you going:

#include <File.au3>

Local $aRecords

$aFileList = _FileListToArray("D:WorkDir\DataSources\Output\", "*.xml", 1)
$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'

For $x = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$x], $aRecords)
For $y = 0 To UBound($aRecords) - 1
StringReplace($aRecords[$y], $StringSearch, $StringReplace)
Next
_FileWriteFromArray($aFileList[$x], $aRecords, 1)
Next
Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

@Nessie: Sorry about it , apology! :) Here's a scenario:

1. I have a lot of XMLs which contains the above strings

2. I need to search for the above strings in all xml files and replace it in the desired strings.

3. After that, save all xml files.

@Nitekram: Sorry but not working. Thanks.

Link to comment
Share on other sites

try this:

EDIT let me do some testings

#include <File.au3>

Local $aRecords

$aFileList = _FileListToArray("D:WorkDir\DataSources\Output\", "*.xml", 1)
$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'

For $x = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$x], $aRecords)
For $y = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$y], $StringSearch) Then
StringReplace($aRecords[$y], $StringSearch, $StringReplace)
EndIf
Next
_FileWriteFromArray($aFileList[$x], $aRecords)
Next
Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

You may want to test this on a small subset, and also NEVER test on your live data

#include <File.au3>
Local $aRecords
Local $path = "D:WorkDir\DataSources\Output\"

$aFileList = _FileListToArray($path, "*.xml", 1)
If @error Then MsgBox('','@error',@error)

$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'

For $x = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$x], $aRecords)
For $y = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$y], $StringSearch) Then
MsgBox('',$aRecords[$y],'found')
;Exit
$aRecords[$y] = StringReplace($aRecords[$y], $StringSearch, $StringReplace)
MsgBox('','',$aRecords[$y])
EndIf
Next
_FileWriteFromArray($aFileList[$x], $aRecords, 1)
Next

edit changed the the parameteres for filewritefromarray - as it was putting a 1 on the top line

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Tested and working:

#include <File.au3>

$xml_file = _FileListToArray(@ScriptDir, "*.xml", 1)
If @error Then
    MsgBox(16, "Error", "No xml file")
    Exit
EndIf

For $i = 1 To UBound($xml_file) - 1
    $xml_path = @ScriptDir & "\" & $xml_file[$i]
    _ReplaceStringInFile($xml_path, '<EqSource Format="TEX">', '<Source Format="LATEX">')
    _ReplaceStringInFile($xml_path, '</EqSource>', "</Source>")
Next

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

You may want to test this on a small subset, and also NEVER test on your live data

#include <File.au3>
Local $aRecords
Local $path = "D:WorkDir\DataSources\Output\"

$aFileList = _FileListToArray($path, "*.xml", 1)
If @error Then MsgBox('','@error',@error)

$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'

For $x = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$x], $aRecords)
For $y = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$y], $StringSearch) Then
MsgBox('',$aRecords[$y],'found')
;Exit
$aRecords[$y] = StringReplace($aRecords[$y], $StringSearch, $StringReplace)
MsgBox('','',$aRecords[$y])
EndIf
Next
_FileWriteFromArray($aFileList[$x], $aRecords, 1)
Next

edit changed the the parameteres for filewritefromarray - as it was putting a 1 on the top line

Thanks! It's now working, but how about the </EqSource> to </Source>?

I have tried this one but not working:

#include <File.au3>
Local $aRecords
Local $path = "D:WorkDir\DataSources\Output\"

$aFileList = _FileListToArray($path, "*.xml", 1)
If @error Then MsgBox('','@error',@error)

$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'

For $x = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$x], $aRecords)
For $y = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$y], $StringSearch) Then
MsgBox('',$aRecords[$y],'found')
;Exit
$aRecords[$y] = StringReplace($aRecords[$y], $StringSearch, $StringReplace)
MsgBox('','',$aRecords[$y])
EndIf
Next
_FileWriteFromArray($aFileList[$x], $aRecords, 1)
Next

$StringSearch1 = '<EqSource Format="TEX">'
$StringReplace1 = '= <Source Format="LATEX">'

For $a = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$a], $aRecords)
For $b = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$b], $StringSearch) Then
MsgBox('',$aRecords[$b],'found')
;Exit
$aRecords[$b] = StringReplace($aRecords[$b], $StringSearch, $StringReplace)
MsgBox('','',$aRecords[$b])
EndIf
Next
_FileWriteFromArray($aFileList[$a], $aRecords, 1)
Next
Link to comment
Share on other sites

Thanks! It's now working, but how about the </EqSource> to </Source>?

I have tried this one but not working:

#include <File.au3>
Local $aRecords
Local $path = "D:WorkDir\DataSources\Output\"

$aFileList = _FileListToArray($path, "*.xml", 1)
If @error Then MsgBox('','@error',@error)

$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'

For $x = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$x], $aRecords)
For $y = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$y], $StringSearch) Then
MsgBox('',$aRecords[$y],'found')
;Exit
$aRecords[$y] = StringReplace($aRecords[$y], $StringSearch, $StringReplace)
MsgBox('','',$aRecords[$y])
EndIf
Next
_FileWriteFromArray($aFileList[$x], $aRecords, 1)
Next

$StringSearch1 = '<EqSource Format="TEX">'
$StringReplace1 = '= <Source Format="LATEX">'

For $a = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$a], $aRecords)
For $b = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$b], $StringSearch) Then
MsgBox('',$aRecords[$b],'found')
;Exit
$aRecords[$b] = StringReplace($aRecords[$b], $StringSearch, $StringReplace)
MsgBox('','',$aRecords[$b])
EndIf
Next
_FileWriteFromArray($aFileList[$a], $aRecords, 1)
Next

Have you tried my code?

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

I did not know about that function - I learn something again, as I really have never used any of these functions, but thought since OP was using I would search for them and learn some more...thanks

EDIT

Here is the second line item

#include <File.au3>
#include <array.au3>
Local $aRecords
Local $path = "D:WorkDir\DataSources\Output\"

$aFileList = _FileListToArray($path, "*.xml", 1)
If @error Then MsgBox('','@error',@error)

$StringSearch = '<EqSource Format="TEX">'
$StringReplace = '= <Source Format="LATEX">'
$StringSearch2 = '</EqSource>',
$StringReplace2 = "</Source>")

For $x = 0 To UBound($aFileList) - 1
_FileReadToArray($aFileList[$x], $aRecords)
For $y = 0 To UBound($aRecords) - 1
If StringInStr($aRecords[$y], $StringSearch) Then
$aRecords[$y] = StringReplace($aRecords[$y], $StringSearch, $StringReplace)
EndIf
If StringInStr($aRecords[$y], $StringSearch2) Then
$aRecords[$y] = StringReplace($aRecords[$y], $StringSearch2, $StringReplace2)
endif

Next
_FileWriteFromArray($aFileList[$x], $aRecords, 1)
Next
Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Tested and working:

#include <File.au3>

$xml_file = _FileListToArray(@ScriptDir, "*.xml", 1)
If @error Then
    MsgBox(16, "Error", "No xml file")
    Exit
EndIf

For $i = 1 To UBound($xml_file) - 1
    $xml_path = @ScriptDir & "\" & $xml_file[$i]
    _ReplaceStringInFile($xml_path, '<EqSource Format="TEX">', '<Source Format="LATEX">')
    _ReplaceStringInFile($xml_path, '</EqSource>', "</Source>")
Next

Hi!

Hello Nessie,

This is working great! Thanks.

Link to comment
Share on other sites

I did not know about that function - I learn something again, as I really have never used any of these functions, but thought since OP was using I would search for them and learn some more...thanks

Thank you for your help Mark. I really appreciate it and I've learned a lot from you.

@Nessie, thanks for your quick response. This will help me a lot! A big thanks to you guys!

CHEERS

Link to comment
Share on other sites

Thank you for your help Mark. I really appreciate it and I've learned a lot from you.

@Nessie, thanks for your quick response. This will help me a lot! A big thanks to you guys!

CHEERS

Glad to help you. Everyone have to learn from each other ;)

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

@Nessie

One more thing. How do I delete the entire tag/element from a multiple xml? Below is the xml element to be deleted:

<Eq Source="LATEX"><![CDATA[ $$begin{array}{*{20}l} Kw+Pa &= V, P^Tw &= 0, end{array} $$ ]]>></Eq>

Please remember that there are different CDATA in one xml. Thanks in advance.

Edited by aguynamedray
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...