Parsing XML with Tasker

3,749 views
Skip to first unread message

francwalter

unread,
Oct 10, 2013, 7:14:21 AM10/10/13
to tas...@googlegroups.com
Hello

I just want to create a little Zoom-Widget to get the actual weather from the great and free openweather.com API:

http://www.openweathermap.org/API

And I get some nice XML if I use e.g. this:

http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml

but now I need to parse it with Tasker. On the todo page of Tasker (http://tasker.dinglisch.net/todo.html) I found this line:

parse XML to pull out specified bits (tag=xxx attrname=yyy attrval=zzz etc), or is search-replace pattern-matching sufficient ?

Is this still a todo or can I find the feature somewhere?
Do I best need Variable Split if no xml parsing is available?

Thank you,

frank

Tasker 4.2b8 on Samsung S2 with 4.1.2

Pent

unread,
Oct 10, 2013, 8:13:46 AM10/10/13
to tas...@googlegroups.com

Is this still a todo or can I find the feature somewhere?
Do I best need Variable Split if no xml parsing is available?


XML parsing is still todo. Variable Search Replace is probably better if you know or can
be bothered to learn about regular expressions.

Pent

francwalter

unread,
Oct 10, 2013, 10:54:28 AM10/10/13
to tas...@googlegroups.com

I would like to use Search Replace, but I don't find any example how this really works.
E.g. in a %HTTPD I try to find this:

<span id="localtime">(.*)</span>

In my Variable Search Replace as "Store Matches In" I set: %zeitaktuell, and now I hope that I get in this variable:

Aktuelle Zeit: Do, 10.10. 15:38 MESZ

because in the Source I read:

<span id="localtime">Aktuelle Zeit: Do, 10.10. 15:38 MESZ </span>

But I don't get the value, I get the name of the variable "%zeitaktuell", instead of the (only) match.
Even if there where no result, this variable should be empty at all.

In:

http://www.pocketables.com/2012/09/beginners-guide-to-tasker-part-4-variable-data-processing.html

I read only few lines about Search and Replace, and in:

http://tasker.dinglisch.net/userguide/en/matching.html

I don't find anything about the "Store Matches In" at all.
Is there any working example for this function (search and replace)?

Thanks.

frank



 
Message has been deleted

Bob Hansen

unread,
Oct 10, 2013, 12:58:15 PM10/10/13
to tas...@googlegroups.com
First when a variable is empty and is viewed, Tasker will show the empty variable name.

For extracting the desired text, it would be easier to use variable split.

Assuming the xml to be parsed is in variable %data
To split this line:

    <span id="localtime">Aktuelle Zeit: Do, 10.10. 15:38 MESZ </span>

Use:
    Variable Split: %data Splitter: "localtime"> 

This will result in %data2 containing 
    
    Aktuelle Zeit: Do, 10.10. 15:38 MESZ </span>

Then do a second variable split on %data2 

    Variable split: %data2  Splitter: </

This will result in %data21 containing

    Aktuelle Zeit: Do, 10.10. 15:38 MESZ 

Hope that helps...

francwalter

unread,
Oct 10, 2013, 2:56:12 PM10/10/13
to tas...@googlegroups.com
Am Donnerstag, 10. Oktober 2013 18:58:15 UTC+2 schrieb Bob Hansen:
...
For extracting the desired text, it would be easier to use variable split.
...

Thank you, I did this already with split and it works. But as Pent says, a better and less complicated way would be with Search Replace - if this worked.

@Pent: Please, could you indicate me a working example for the regex-replacement in Search Replace somewhere?
Or could you update the help files (if existing)?

frank

Mike L

unread,
Oct 10, 2013, 3:18:25 PM10/10/13
to tas...@googlegroups.com
If the original source if this:

<span id="localtime">Aktuelle Zeit: Do, 10.10. 15:38 MESZ </span>

then the following regex would pull "Aktuelle Zeit: Do, 10.10. 15:38 MESZ "

(?<=<span id="localtime">).+(?=</span>)

As for the "Store Matches In" field, it will always populate whatever variable as an array even if there is only one match. So you will need to use %match(:) or %match(1) to access what the regex has found.

I use this site here to troubleshoot my expressions, has a nice little cheat sheet on the right to reference from.

Frank Röhm

unread,
Oct 10, 2013, 6:42:41 PM10/10/13
to tas...@googlegroups.com


Mike L <kitpri...@gmail.com> schrieb:
>If the original source if this:
>
><span id="localtime">Aktuelle Zeit: Do, 10.10. 15:38 MESZ </span>
>...
>(?<=<span id="localtime">).+(?=</span>)
>
>As for the "Store Matches In" field, it will always populate whatever
>variable as an array even if there is only one match. So you will need
>to
>use %match(:) or %match(1) to access what the regex has found.
>...

I still don't understand it or it doesn't work.
In a very simple example task I tried to show a match in a popup:

1. Variable set:
%text = This is a simple test

2. Variable search replace:
Variable = %text
search = is.*simple
Store Matches In = %Result

3. Popup
Text = %Result(0)

But still I get only:

%Result(0)

And not:

is a simple

In my Popup.
So what is my error?
And can't I use the content of the parenthesis like in normal RegEx?
E.g.: $1 or \1

Still the question, where can I find a working example?
Is Tasker too powerful that there can't be a complete documenation for all functions?
Would be so helpful!

Frank

Mike L

unread,
Oct 10, 2013, 7:01:44 PM10/10/13
to tas...@googlegroups.com
I gave you a working example of regex...
Tasker arrays start at 1, not 0 like a lot of programming languages. %Result(1) should flash what you want in your example. 

And yes $x should work fine, but (as far as I can tell) the captures are only available in that action, ie you can use them in the "Replace With" parameter to rearrange things or whatever and that's it. Here is a working example:

<F° = Degrees>
A1: Variable Search Replace [ Variable:%weather Search:(\d{1,3})(F) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In: Replace Matches:On Replace With:$1 degrees ] 

Frank Röhm

unread,
Oct 10, 2013, 7:06:24 PM10/10/13
to tas...@googlegroups.com


Mike L <kitpri...@gmail.com> schrieb:
...
Great! Thank you!
The array starts with 1! That was my error.
Thank you for the complete example!
Good night, all is clear now.

Frank

Frank Röhm

unread,
Oct 11, 2013, 1:40:07 AM10/11/13
to tas...@googlegroups.com


Mike L <kitpri...@gmail.com> schrieb:
>...And yes $x should work fine, but (as far as I can tell) the captures
>are
>only available in that action...
>Here is a working example:
>...

I tried myself the replacement and like you said this works in the same action, here my example (xml export):

<TaskerData sr="" dvi="1" tv="4.2b8">
<Task sr="task21">
<cdate>1381443340701</cdate>
<edate>1381469761709</edate>
<id>21</id>
<nme>TestSearch</nme>
<pri>10</pri>
<Action sr="act0" ve="3">
<code>547</code>
<Str sr="arg0" ve="3">%text</Str>
<Str sr="arg1" ve="3">The color is a BLUE color</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act1" ve="3">
<code>598</code>
<Str sr="arg0" ve="3">%text</Str>
<Str sr="arg1" ve="3">is (a) (.*) (color)</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
<Int sr="arg4" val="0"/>
<Str sr="arg5" ve="3"/>
<Int sr="arg6" val="1"/>
<Str sr="arg7" ve="3">was $2 but is now $1 RED $3</Str>
</Action>
<Action sr="act2" ve="3">
<code>550</code>
<Str sr="arg0" ve="3"/>
<Str sr="arg1" ve="3">%text</Str>
<Str sr="arg2" ve="3"/>
<Str sr="arg3" ve="3">Popup</Str>
<Int sr="arg4" val="3"/>
<Int sr="arg5" val="1"/>
</Action>
</Task>
</TaskerData>

Jay M

unread,
Oct 11, 2013, 1:58:58 AM10/11/13
to tas...@googlegroups.com

Export description to clipboard

Jay M

--
You received this message because you are subscribed to the Google Groups "Tasker" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tasker+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/tasker.
For more options, visit https://groups.google.com/groups/opt_out.

Frank Röhm

unread,
Oct 11, 2013, 2:07:41 AM10/11/13
to tas...@googlegroups.com


Jay M <jaymen...@googlemail.com> schrieb:
>Export description to clipboard
>

TestSearch (21)
A1: Variable Set [ Name:%text To:The color is a BLUE color Do Maths:Off Append:Off ]
A2: Variable Search Replace [ Variable:%text Search:is (a) (.*) (color) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In: Replace Matches:On Replace With:was $2 but is now $1 RED $3 ]
A3: Popup [ Title: Text:%text Background Image: Layout:Popup Timeout (Seconds):3 Show Over Keyguard:On ]

francwalter

unread,
Oct 13, 2013, 12:16:51 PM10/13/13
to tas...@googlegroups.com

Could I participate in building a beautiful XML-Parser for Tasker?
Would be an interesting job :)

frank

Achaiah A

unread,
Oct 13, 2013, 4:06:49 PM10/13/13
to tas...@googlegroups.com
This may actually be possible with javascript.  You should be able to use something like this JS library: http://nfarina.com/post/34302964969/a-lightweight-xml-document-class-for-nodejs-javascript and write a scriplet to parse everything you need.  What do you think?

P.S. Pent if you're reading this... Tasker-JS interaction is pretty spotty (I constantly run into functions that fail, don't execute correctly, don't have the same range of inputs as tasker counterparts etc etc).  It would be awesome if we could express tasks in JS as it would streamline a lot of development.  I'd be happy to contribute whatever is needed to make it work.

David Wang

unread,
Oct 13, 2013, 9:42:31 PM10/13/13
to tas...@googlegroups.com
It will be much better to parse XML with XPath. Compared with shell scripts and regexp, it will be much strict and accurate.

You can easily get XPath of specific elements with Chrome browser("Web Inspector") or first object XML editor.

Please consider the XPath way for parsing XML first.

YT

unread,
Dec 21, 2014, 1:58:11 AM12/21/14
to tas...@googlegroups.com
Thanks for the tip - I've managed to locate the XPath to the specific values I want from a webpage, but can you tell me what script I should use to put this into a Tasker variable?

For instance:

var document = global("HTTPD");
var XPathRes = document.evaluate('XPath', document, null, 2, null) 

isn't working for me

David Wang

unread,
Dec 21, 2014, 9:39:08 PM12/21/14
to tas...@googlegroups.com
It seems that XPath doesn't support specific android(browser) versions(Android 2.2,2.3 ??? I'm not sure). Please refer here: http://stackoverflow.com/questions/2688332/xpath-evaluating-error-in-android

Sorry, I only know a little about javascript. 

Wern-Yuen Tan

unread,
Dec 21, 2014, 9:41:21 PM12/21/14
to tas...@googlegroups.com
Thanks - I managed to solve this through a workaround using Yahoo pipes to parse the XPath into a JSON string

--
You received this message because you are subscribed to a topic in the Google Groups "Tasker" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tasker/mrZmADN2Jnc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tasker+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

francwalter

unread,
Mar 30, 2016, 3:57:14 AM3/30/16
to Tasker
Am Donnerstag, 10. Oktober 2013 13:14:21 UTC+2 schrieb francwalter:
...

but now I need to parse it with Tasker. On the todo page of Tasker (http://tasker.dinglisch.net/todo.html) I found this line:

parse XML to pull out specified bits (tag=xxx attrname=yyy attrval=zzz etc), or is search-replace pattern-matching sufficient ?


Now I see a bit better, that this todo list is huge!!!
It seems very unlikely that one day XML-Parser will be implemented :(
So maybe the only way is via JS.

Juergen Gruen

unread,
Mar 30, 2016, 12:17:20 PM3/30/16
to Tasker
Afaik, OpenWeatherMaps supports JSON natively also, which is easier to use with JS, imho...

francwalter

unread,
Mar 30, 2016, 1:23:42 PM3/30/16
to Tasker


Am Mittwoch, 30. März 2016 18:17:20 UTC+2 schrieb Juergen Gruen:
Afaik, OpenWeatherMaps supports JSON natively also, which is easier to use with JS, imho...


Whith what to parse best?

 

Juergen Gruen

unread,
Mar 30, 2016, 5:03:06 PM3/30/16
to Tasker
JS example without any error handling:

var http = new XMLHttpRequest();
var url = "http://api.openweathermap.org/data/2.5/weather?q=london,uk&units=metric&APPID=xxxYourApiIDxxx";
http
.open("GET", url, false);
http
.send();
var data = JSON.parse(http.responseText);

// get current temperature
var temp = data.main.temp;

// get current windspeed
var speed = data.wind.speed;

// get description
var description = data.weather[0].description;

JSON data example
{
 
"coord": {
   
"lon": -0.13,
   
"lat": 51.51
 
},
 
"weather": [
   
{
     
"id": 803,
     
"main": "Clouds",
     
"description": "broken clouds",
     
"icon": "04n"
   
}
 
],
 
"base": "cmc stations",
 
"main": {
   
"temp": 7.48,
   
"pressure": 1010,
   
"humidity": 66,
   
"temp_min": 5,
   
"temp_max": 10
 
},
 
"wind": {
   
"speed": 3.6,
   
"deg": 210
 
},
 
"clouds": {
   
"all": 76
 
},
 
"dt": 1459370673,
 
"sys": {
   
"type": 1,
   
"id": 5091,
   
"message": 0.0052,
   
"country": "GB",
   
"sunrise": 1459316259,
   
"sunset": 1459362752
 
},
 
"id": 2643743,
 
"name": "London",
 
"cod": 200
}


Reply all
Reply to author
Forward
0 new messages