Insert text in xml-file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
yardley
Posts: 30
Joined: 05 May 2021, 21:16

Insert text in xml-file

05 May 2021, 21:26

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!
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Insert text in xml-file  Topic is solved

05 May 2021, 22:01

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%"
yardley
Posts: 30
Joined: 05 May 2021, 21:16

Re: Insert text in xml-file

06 May 2021, 10:48

mikeyww wrote:
05 May 2021, 22:01

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%"
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 format

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>
[Mod edit: codeboxes added.]


Really grateful for your help :)
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Insert text in xml-file

06 May 2021, 11:10

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%"
yardley
Posts: 30
Joined: 05 May 2021, 21:16

Re: Insert text in xml-file

06 May 2021, 11:39

Amazing, thank you so much!
yardley
Posts: 30
Joined: 05 May 2021, 21:16

Re: Insert text in xml-file

06 May 2021, 14:44

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?
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: Insert text in xml-file

06 May 2021, 15:59

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%"
A summary: FileAppend appends to files; StrReplace replaces strings!

Enjoy.
yardley
Posts: 30
Joined: 05 May 2021, 21:16

Re: Insert text in xml-file

06 May 2021, 18:24

Thanks again!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Lamron750, septrinus and 242 guests