Xml

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Xml

14 Jan 2015, 04:12

I have this xml file

Code: Select all

<?xml version="1.0"?>
<mame>
	<game name="wrally">
		<description>World Rally</description>
	</game>
	<game name="kungfum">
		<description>KungFu Master</description>
	</game>
	<game name="commando">
		<description>Commando</description>
	</game>
</mame>
With this code

Code: Select all

FileName := "c:testLista.xml"
file := FileOpen(FileName, "r", "UTF-8")
xmldata := File.Read(File.Length)
I read the xml file ant put the information into the xmldata.

Now, I need put all Description data of xmldata in a Listbox. How can I do it?
How can I walk all data of xmldata from the first until the last one?

Thanks in advance!
Everything is possible!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Xml

14 Jan 2015, 05:11

try this:

Code: Select all

fileName = "c:\testLista.xml"

xmlObj := ComObjCreate( "MSXML2.DOMDocument.6.0" ), xmlObj.load( fileName )

for nodeItem in ( xmlObj.selectNodes( "//description" ), descList := "" )
    descList .= nodeItem.text "|"

Gui, Add, ListBox,, % RTrim( descList, "|" )
Gui, Show
return

GuiClose:
ExitApp
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

14 Jan 2015, 05:48

Thanks!

Now, I want when I select an item of my listbox (select description), the program returns the corresponding name.
I'm trying this

Code: Select all

fileName := "c:\testLista.xml"

xmlObj := ComObjCreate( "MSXML2.DOMDocument.6.0" )
xmlObj.load( fileName )

for nodeItem in ( xmlObj.selectNodes( "//description" ), descList := "" )
    descList .= nodeItem.text "|"

Gui, Add, ListBox,Choose1 vChoice gxxx, % RTrim( descList, "|" )
Gui, Show
return

xxx:
Gui, Submit, NoHide
juegoX = %Choice%
gameNode3 :=doc.selectSingleNode("description='" juegoX "'")
decText3 := gameNode3.selectSingleNode("//game/name").text
msgbox % decText3
return

GuiClose:
ExitApp
any idea?

Thanks in advance.
Everything is possible!
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Xml

14 Jan 2015, 06:20

empardopo wrote:Thanks!

Now, I want when I select an item of my listbox (select description), the program returns the corresponding name.
I'm trying this

Code: Select all

fileName := "c:\testLista.xml"

xmlObj := ComObjCreate( "MSXML2.DOMDocument.6.0" )
xmlObj.load( fileName )

for nodeItem in ( xmlObj.selectNodes( "//description" ), descList := "" )
    descList .= nodeItem.text "|"

Gui, Add, ListBox,Choose1 vChoice gxxx, % RTrim( descList, "|" )
Gui, Show
return

xxx:
Gui, Submit, NoHide
juegoX = %Choice%
gameNode3 :=doc.selectSingleNode("description='" juegoX "'")
decText3 := gameNode3.selectSingleNode("//game/name").text
msgbox % decText3
return

GuiClose:
ExitApp
hope you don't get me wrong, but how do you try that? I mean you can try and try but you have to try with some background about it, if you read some information here and there and have a good idea you can try and fail better

I mean it's simple yet you have to look at the xpath and xml documentations to get around how you can get what you want
gameNode3 :=doc.selectSingleNode("description='" juegoX "'")
[/quote]
btw: where did you get docfrom

Code: Select all

here is what you want:
xxx:
Gui, Submit, NoHide
juegoX = %Choice%
path=//description[text()="%juegoX%"] ; this is the xpath to get the node with the name description and its text is what in the variable juegoX
node:=xmlObj.selectSingleNode(path).parentnode ; we want its parent since its parent that has the name we want
name := node.getattribute("name") ; the name is an attribute no a text so to get the attribute we use GetAttribute method, you can't know that without reading learning about it
msgbox % Name
return
I hope this shed some light on what you want to do, and help you to get some information about it
good luck
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

14 Jan 2015, 06:48

I mix an old code with the new code.
In my old code I had

Code: Select all

doc := ComObjCreate("MSXML2.DOMDocument.6.0")
doc.async := false
doc.loadXML(xmldata)
Sorry.

Now, It works fine!!!! Thanks very very much for your help.

Any good links where I can get a good xml documentations?

Thanks very much, again.
Everything is possible!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Xml

14 Jan 2015, 06:52

empardopo wrote:Now, I want when I select an item of my listbox (select description), the program returns the corresponding name.
...
any idea?
Thanks in advance.

Code: Select all

fileName = c:\testLista.xml

xmlObj := ComObjCreate( "MSXML2.DOMDocument.6.0" ), xmlObj.load( fileName )

for nodeItem in ( xmlObj.selectNodes("//description"), descList := "" )
    descList .= nodeItem.text "|"

Gui, Add, ListBox, vChoice gxxx, % RTrim( descList, "|" )
Gui, Show
return

xxx:
Gui, Submit, NoHide
decText3 := xmlObj.selectSingleNode( "//description[contains(.,'" Choice "')]" ).parentNode.getAttribute( "name" )
msgbox % decText3
return

GuiClose:
ExitApp
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Xml

14 Jan 2015, 07:10

empardopo wrote:I mix an old code with the new code.
In my old code I had

Code: Select all

doc := ComObjCreate("MSXML2.DOMDocument.6.0")
doc.async := false
doc.loadXML(xmldata)
ok, it's that then, it's the same except that Loadloads a file path and LoadXML loads an XML data (string)
so it's ok, the same thing.

Any good links where I can get a good xml documentations?
you can try with http://www.w3schools.com/xpath/default.asp
and http://www.w3schools.com/dom/default.asp
and there is a wrapper which might give a good idea about xml using AHK
and keep in mind that this "MSXML2.DOMDocument.6.0" is a COM object that you can get its documentations from the MSDN: http://msdn.microsoft.com/en-us/library ... 85%29.aspx
here you get methods and properties...
http://msdn.microsoft.com/en-us/library ... 85%29.aspx
which is really good to understand anything you can or want to do
and there is more and more google some methods or functions for a better underspending
once you have a feel of how it works and how to work with it, the rest is easy
about the wrapper it good for learning and testing
http://www.autohotkey.com/board/topic/8 ... parse-xml/
Thanks very much, again.
no problem at all
P.S.
I have a script called in a folder called HitAndMiss :D every time I find something or need somthing I use it as refrence and it's terrible code left and right, but there is some comment that help find things sometime
but most of it is just methods how they work, what their params, things like that, so
Create your HitAndMiss script file.
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

14 Jan 2015, 07:16

TLM, thanks for your help. Now I have two ways to do the same. :-)
MJs, thanks very much again. I'll create my HitAndMiss script file :-)
Everything is possible!
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

26 Jan 2015, 07:15

I have read the documentation and I still have doubts ...

I changed my xml file ...

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<mame>
    <game name="wrally">
        <description>World Rally</description>
        <bat>World Rally.bat</bat>
    </game>
    <game name="kungfu">
        <description>KungFu Master</description>
        <bat>KungFuMaster.bat</bat>
    </game>
    <game name="commando">
        <description>Commando</description>
        <bat>Commando.bat</bat>
    </game>
</mame>
Using

Code: Select all

xxx:
Gui, Submit, NoHide
juegoX = %Choice%
path=//description[text()="%juegoX%"] ; this is the xpath to get the node with the name description and its text is what in the variable juegoX
node:=xmlObj.selectSingleNode(path).parentnode ; we want its parent since its parent that has the name we want
name := node.getattribute("name") ; the name is an attribute no a text so to get the attribute we use GetAttribute method, you can't know that without reading learning about it
msgbox % Name
return
I get the name but I need get the bat value...

How can I get it?

Thanks
Everything is possible!
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Xml

26 Jan 2015, 08:16

Code: Select all

xxx:
Gui, Submit, NoHide
juegoX = %Choice%
path=//description[text()="%juegoX%"] ; this is the xpath to get the node with the name description and its text is what in the variable juegoX
node:=xmlDoc2.selectSingleNode(path).parentnode.SelectSingleNode("bat") ; we get the parent, then we get the 
node named bat, and we get its text
; if you are sure that bat Node comes after the description node you can do it like that
;node := xmlDoc2.selectSingleNode(path).nextsibling()  ; well, nextsibling get the next sibling node
NodeText:= node.text
msgbox % NodeText
return
 
with some more reading... :D
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

26 Jan 2015, 09:07

@MJs, Not only must read, but I must also understand! :lol:

Thanks very much.
Everything is possible!
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Xml

26 Jan 2015, 09:18

"but I must also understand"
well, that too. but with time...
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

27 Jan 2015, 06:31

MJs wrote:"but I must also understand"
well, that too. but with time...
Thanks very much again.
Everything is possible!
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

02 Feb 2015, 07:03

Another question.
Having the same xml file

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<mame>
    <game name="wrally">
        <description>World Rally</description>
        <bat>World Rally.bat</bat>
    </game>
    <game name="kungfu">
        <description>KungFu Master</description>
        <bat>KungFuMaster.bat</bat>
    </game>
    <game name="commando">
        <description>Commando</description>
        <bat>Commando.bat</bat>
    </game>
</mame>
I want to put every name in a list. I don't know how can I read this "node". I have used the next code

Code: Select all

;Read xml file and load values into descList
xmlObj := ComObjCreate( "MSXML2.DOMDocument.6.0" )
xmlObj.load( RomsListTemp )

for nodeItem in ( xmlObj.selectNodes( "//description" ), descList := "" )
    descList .= nodeItem.text "|"

for nodeItem in ( xmlObj.selectNodes( "//name" ), descList2 := "" )
    descList2 .= nodeItem.text "|"
Thanks in advance.
Everything is possible!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Xml

02 Feb 2015, 19:24

you can use a bunch of single for-loop's for each item you want:

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//game" ),  descList2 := ""  )
    descList2 .= nodeItem.getAttribute( "name" ) "|"

msgbox % RTrim( descList2, "|" )
or you can also create some association using an array for multiple/related items:

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//description" ), gameArray := {}, d:= n:="" )
    gameArray := { descriptions: d .= nodeItem.text "|", names: n .= nodeItem.parentNode.getAttribute( "name" ) "|" }

msgbox % "Descriptions: " RTrim( gameArray.descriptions, "|" ) "`nGame Names: " RTrim( gameArray.names, "|" )
note: you don't have to init the array and or d, n variables this way.

if you wanted to be a little "safer"

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//game" ), gameArray := {}, d:= n:="" )
{
    gameArray := { descriptions: d .= nodeItem.getElementsByTagName( "description" ).item[ 0 ].text "|"
	             , names: n .= nodeItem.getAttribute( "name" ) "|" }
}

msgbox % "Descriptions: " RTrim( gameArray.descriptions, "|" ) "`nGame Names: " RTrim( gameArray.names, "|" )
the take away here is there are many ways you can approach getting info from xml tags ;)
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Xml

03 Feb 2015, 06:51

do you know about that chinese proverb:
Chinese Proverbs Give me a fish and I eat for a day. Teach me to fish and I eat for a lifetime
so, be sure that every time you want a node (fish) even if you're in the middle of a river :D
I see that you went with TLM code, mine is better it's greener :lol:
all that a side, back to the fish and life:
can you tell what you did wrong?
based on what you wrote and on what TLM wrote, thanks TLM
you need at least to know how to get a node, and collection of nodes, attribute, get a node by attribute or text
so: what was wrong with your code?

PS:
thanks TLM, just kidding, keep the good work
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

04 Feb 2015, 04:26

TLM wrote:you can use a bunch of single for-loop's for each item you want:

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//game" ),  descList2 := ""  )
    descList2 .= nodeItem.getAttribute( "name" ) "|"

msgbox % RTrim( descList2, "|" )
or you can also create some association using an array for multiple/related items:

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//description" ), gameArray := {}, d:= n:="" )
    gameArray := { descriptions: d .= nodeItem.text "|", names: n .= nodeItem.parentNode.getAttribute( "name" ) "|" }

msgbox % "Descriptions: " RTrim( gameArray.descriptions, "|" ) "`nGame Names: " RTrim( gameArray.names, "|" )
note: you don't have to init the array and or d, n variables this way.

if you wanted to be a little "safer"

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//game" ), gameArray := {}, d:= n:="" )
{
    gameArray := { descriptions: d .= nodeItem.getElementsByTagName( "description" ).item[ 0 ].text "|"
	             , names: n .= nodeItem.getAttribute( "name" ) "|" }
}

msgbox % "Descriptions: " RTrim( gameArray.descriptions, "|" ) "`nGame Names: " RTrim( gameArray.names, "|" )
the take away here is there are many ways you can approach getting info from xml tags ;)
Thanks very much.
What is the best way I can approach?

@MJs, you are right. I know this proverb.
In this case, I've read information about xml and I've understood nothing. I'm sure with these examples I'm going to learn very much.

Thank you very much to both.
Everything is possible!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Xml

04 Feb 2015, 10:09

empardopo wrote:What is the best way I can approach?
If you are gathering lists for more than one item from the game nodes,
id say use an associative array containing multiple lists, rather than having to perform multiple loops.

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//game" ), gameArray := {}, d := n := "" )
{
    try gameArray := { descriptions: d .= nodeItem.getElementsByTagName( "description" ).item[ 0 ].text "|"
                     , names: n .= nodeItem.getAttribute( "name" ) "|" }
}

msgbox % "Descriptions: " RTrim( gameArray.descriptions, "|" ) "`nGame Names: " RTrim( gameArray.names, "|" )
also, if you are just going to use these lists in Drop Down GUI Lists, you don't need to trim off the pipe delimiter so you can just use:
gameArray.descriptions or gameArray.names
htms
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Xml

06 Feb 2015, 03:37

TLM wrote:
empardopo wrote:What is the best way I can approach?
If you are gathering lists for more than one item from the game nodes,
id say use an associative array containing multiple lists, rather than having to perform multiple loops.

Code: Select all

for nodeItem in ( xmlObj.selectNodes( "//game" ), gameArray := {}, d := n := "" )
{
    try gameArray := { descriptions: d .= nodeItem.getElementsByTagName( "description" ).item[ 0 ].text "|"
                     , names: n .= nodeItem.getAttribute( "name" ) "|" }
}

msgbox % "Descriptions: " RTrim( gameArray.descriptions, "|" ) "`nGame Names: " RTrim( gameArray.names, "|" )
also, if you are just going to use these lists in Drop Down GUI Lists, you don't need to trim off the pipe delimiter so you can just use:
gameArray.descriptions or gameArray.names
htms
Thanks very much again.
Everything is possible!
eduardoti

Re: Xml

29 Aug 2018, 05:18

[quote="TLM"][/quote]

Can you help me with a code to get this entire line?

Code: Select all

<description>KungFu Master</description>[/code

getElementsByTagName( "description" ).item[ 0 ].text

gives me 
KungFu Master

But how do I get the entire line <description>KungFu Master</description>

I tried outertext but it did not work

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: fiendhunter, jollyjoe, mikeyww, Rohwedder, Scr1pter and 321 guests