RegEx Text Between XML Tags

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

RegEx Text Between XML Tags

19 Dec 2013, 11:30

How can I use RegEx to assign a variable to the text between the opening and closing XML tags? For example, how can I get the text between <units> & </units>?

<units>
<temp>F</temp>
<dist>MI</dist>
<speed>MPH</speed>
<pres>IN</pres>
<prec>IN</prec>
</units>

I have tried searching various forums and found the code below which seems to work, but I'm not exactly sure how it works or if it would be the best/accurate way to complete the task:

Code: Select all

FileDelete, Wx.dat
UrlDownloadToFile, http://thale.accu-weather.com/widget/thale/weather-data.asp?location=37201, Wx.dat
FileRead, xmldata, Wx.dat

RegExMatch(xmldata, "s)<units>(.*)</units>", m)
MsgBox, %m1%
What I plan to do is assign a variable to each and every item from the XML file, which sometimes has tags using the same name. Figuring out how to work with that will be another challenge.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: RegEx Text Between XML Tags

19 Dec 2013, 12:54

This handles a lot of the repeating tags.

Code: Select all

#Persistent
xml := URL_ToVar("http://thale.accu-weather.com/widget/thale/weather-data.asp?location=37201")
while p := RegExMatch(xml, "Us)<day number=""(\d)"">(.+)</day>", m, p?p+1:1) {	;Search "<day number="x">...</day number="x">
	RegExMatch(m2, "s)<daytime>.+</daytime>", d)								;<daytime>...</daytime>
		day%m1%_daytime := d
	RegExMatch(m2, "s)<nighttime>.+</nighttime>", n)							;<nighttime>...</nighttime>
		day%m1%_nighttime := n
}
Loop, 5 {
	i := A_Index, p :=0
	while p := RegExMatch(day%i%_daytime, "Us)<([^/]+)>", m, p?p+1:1) 			;<tags>
		if RegExMatch(day%i%_daytime, "U)<.+>\K.+(?=</" m1 ">)", s, p)			;<tag>...</tag>
			Day%i%_daytime_%m1% := s
	p :=0
	while p := RegExMatch(day%i%_nighttime, "Us)<([^/]+)>", m, p?p+1:1)			;<tags>
		if RegExMatch(day%i%_nighttime, "U)<.+>\K.+(?=</" m1 ">)", s, p)		;<tag>...</tag>
			Day%i%_nighttime_%m1% := s
}
ListVars
return

URL_ToVar(URL) {  
    WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    WebRequest.Open("GET", URL)
    WebRequest.Send()
    return, WebRequest.ResponseText
}

Esc::
ExitApp
Edit: See these two links to learn more about how RegEx works: RegExMatch and RegEx Quick Reference. If you have any questions don't hesitate to ask.
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: RegEx Text Between XML Tags

21 Dec 2013, 15:57

Code: Select all

xmldata =
(
<units>
	<temp>F</temp>
	<dist>MI</dist>
	<speed>MPH</speed>
	<pres>IN</pres>
	<prec>IN</prec>
</units>
)
doc := loadXML(xmldata)
nodes := doc.selectSingleNode("/units")

MsgBox % nodes.text

for node in nodes.childNodes
	t .= node.NodeName " :`t" node.text "`n"
MsgBox %t%


loadXML(ByRef data)
{
	o := ComObjCreate("MSXML2.DOMDocument.6.0")
	o.async := false
	o.loadXML(data)
	return o
}
See COM Object Reference: MSXML2.DOMDocument.6.0 for more info.
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: RegEx Text Between XML Tags

21 Dec 2013, 18:31

@.com post http://www.autohotkey.com/board/topic/9 ... ntry625747

Hey if you use the XML Com object you don't need to but it all in to varibles as the object wil hold them all you just need to know more about how to get them out

There is more then one way to do that to...
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: roeleboele and 383 guests