Hi all, I am trying to insert text in a file with extension .xml.
Currently it looks like this:
<name="Example1" />
<name="Example2" />
<name="Example3" />
<name="Example4" />
<name="Example5" />
<name="Example6" />
I want to insert a new entry after Example 3 like this:
<name="Example1" />
<name="Example2" />
<name="Example3" />
<name="NEW ENTRY HERE" />
<name="Example4" />
<name="Example5" />
<name="Example6" />
Hoping someone can help me, thanks!
Insert text in xml-file Topic is solved
Re: Insert text in xml-file Topic is solved
Code: Select all
FileRead, xml, % file := A_ScriptDir "\test.xml"
FileRecycle, %file%
FileAppend, % StrReplace(xml, append := "<name=""Example3"" />`r`n"
, append "<name=""NEW ENTRY HERE"" />`r`n"), %file%
Run, notepad.exe "%file%"
Re: Insert text in xml-file
Thank you so much! This works great. I was trying to modify the code to insert a larger section of text and failed miserably. I have xml files with the following formatmikeyww wrote: ↑05 May 2021, 22:01Code: Select all
FileRead, xml, % file := A_ScriptDir "\test.xml" FileRecycle, %file% FileAppend, % StrReplace(xml, append := "<name=""Example3"" />`r`n" , append "<name=""NEW ENTRY HERE"" />`r`n"), %file% Run, notepad.exe "%file%"
Code: Select all
<name="test name1" index="" image="">
<description>test description</description>
<cloneof></cloneof>
<crc></crc>
<manufacturer>test manufacturer</manufacturer>
<year>2009</year>
<genre>test genre</genre>
<rating>Other - NR (Not Rated)</rating>
<enabled></enabled>
</name>
<name="test name2" index="" image="">
<description>test description2</description>
<cloneof></cloneof>
<crc></crc>
<manufacturer>test manufacturer2</manufacturer>
<year>2009</year>
<genre>test genre2</genre>
<rating>Other - NR (Not Rated)</rating>
<enabled></enabled>
</name>
<name="test name3" index="" image="">
<description>test description3</description>
<cloneof></cloneof>
<crc></crc>
<manufacturer>test manufacturer3</manufacturer>
<year>2009</year>
<genre>test genre3</genre>
<rating>Other - NR (Not Rated)</rating>
<enabled></enabled>
</name>
I would like to insert the following into the above file after the second "test name2" entry above:
Code: Select all
<name="new name" index="" image="">
<description>new description</description>
<cloneof></cloneof>
<crc></crc>
<manufacturer>new manufacturer</manufacturer>
<year>2009</year>
<genre>new genre</genre>
<rating>Other - NR (Not Rated)</rating>
<enabled></enabled>
</name>
Really grateful for your help
Re: Insert text in xml-file
Code: Select all
str =
( Join`r`n
<name="new name" index="" image="">
<description>new description</description>
<cloneof></cloneof>
<crc></crc>
<manufacturer>new manufacturer</manufacturer>
<year>2009</year>
<genre>new genre</genre>
<rating>Other - NR (Not Rated)</rating>
<enabled></enabled>
</name>
)
FileRead, xml, % file := A_ScriptDir "\test.xml"
FileRecycle, %file%
FileAppend, % StrReplace(xml, prepend := "<name=""test name3"" index="""" image="""">`r`n"
, str "`n`t" prepend), %file%
Run, notepad.exe "%file%"
Re: Insert text in xml-file
Amazing, thank you so much!
Re: Insert text in xml-file
Can I trouble you one last time? If I wanted to insert the text from the previous example at the very end of the xml file regardless of what came before it what would I change?
Re: Insert text in xml-file
Code: Select all
str =
( Join`r`n
<name="new name" index="" image="">
<description>new description</description>
<cloneof></cloneof>
<crc></crc>
<manufacturer>new manufacturer</manufacturer>
<year>2009</year>
<genre>new genre</genre>
<rating>Other - NR (Not Rated)</rating>
<enabled></enabled>
</name>
)
FileAppend, `r`n`t%str%, % file := A_ScriptDir "\test.xml"
Run, notepad.exe "%file%"
Enjoy.
Re: Insert text in xml-file
Thanks again!