[Solved] Simply Add a Node in XML File

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
krabat
Posts: 8
Joined: 04 May 2015, 06:14

[Solved] Simply Add a Node in XML File

25 May 2015, 14:44

Hi,

I have this XML file :

Code: Select all

<menu>
  <game name="Sega Mega Drive"/>
  <game name="Capcom Play System"/>
  <game name="Capcom Play System 2"/>
  <game name="Capcom Play System 3"/>
</menu>
I simply want to add a node at the end of the file : <game name="Super Nintendo"/>

I know how to read the XML File:

Code: Select all

FileRead, xmldata, %A_ScriptDir%\DataBases\Main Menu\Main Menu.xml ; Set xmldata to contents of file 
doc := ComObjCreate("MSXML2.DOMDocument.6.0")
doc.async := false
doc.loadXML(xmldata)
DocNode := doc.selectSingleNode("//menu/game").getAttribute("name")
I know how to save or UPDATE existing datas :

Code: Select all

doc.save(A_ScriptDir . "\DataBases\Main Menu\Main Menu.xml ")
THANK YOU for helping me for this ... i have spent lots of hours to reach this point and i admit that it's not easy to finish the last step !
Last edited by krabat on 26 May 2015, 00:39, edited 1 time in total.
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Simply Add a Node in XML File

25 May 2015, 16:40

Code: Select all

xml =
(
<menu>
  <game name="Sega Mega Drive"/>
  <game name="Capcom Play System"/>
  <game name="Capcom Play System 2"/>
  <game name="Capcom Play System 3"/>
</menu>
)


oXML := ComObjCreate("MSXML2.DOMDocument.6.0")
oXML.async := false
oXML.loadXML(xml)

oNode := oXML.createElement("game")
oXML.getElementsByTagName("menu").Item("0").appendChild(oNode)
oNode.setAttribute("name", "Super Nintendo")

msgbox % oXML.xml
return
Hope it helps
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
krabat
Posts: 8
Joined: 04 May 2015, 06:14

Re: Simply Add a Node in XML File

26 May 2015, 00:15

Hi,

Thank you for your reply but ...

I simply want to add a node at the end of the file : <game name="Super Nintendo"/>

And i'm looking for the "magical words" that can do it !

Maybe something like DocNode.add (<game name="Super Nintendo"/>) ...
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Simply Add a Node in XML File

26 May 2015, 00:23

You did nok seem to scrol down over my code example there is more then just the plain XML :)
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
krabat
Posts: 8
Joined: 04 May 2015, 06:14

[Solved] Simply Add a Node in XML File

26 May 2015, 00:38

Blackholyman wrote:You did nok seem to scrol down over my code example there is more then just the plain XML :)
Sorry, i did not seen the scroll down ...

THis is perfect, now with your scroll i can launch magical spells like this ! :clap:

Regards from Paris,

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Google Adsense [Bot], peter_ahk, Ragnar, septrinus, yuu453 and 286 guests