It is currently April 28th, 2024, 4:55 pm

XML Parsing

Get help with creating, editing & fixing problems with skins
StallSpin
Posts: 5
Joined: December 11th, 2012, 6:13 am

XML Parsing

Post by StallSpin »

I'm trying to get a Measure to parse an XML file from a site. Sounds easy enough, but some of the tags are not constant. Sometimes there are multiples of the same tag. For example:

Code: Select all

<sky_condition sky_cover="SCT" cloud_base_ft_agl="3500"/>
<sky_condition sky_cover="OVC" cloud_base_ft_agl="4900"/>
This is showing currently, but there can be up to 4 of the <sky_condition ... /> tags, or there can be zero.

Here's what that section might look like in my ski:

Code: Select all

[Variables]
URL="http://abcd"
SkyCover=sky_cover="(.*)"
CloudBase=cloud_base_ft_agl="(.*)"

[MeasureWebsite]
Measure=Plugin
Plugin=WebParser
UpadateRate=1800
URL=#URL#
RegExp="(?siU)#SkyCover#.*#CloudBase#"

[MeasureSkyCover]
Measure=Plugin
Plugin=WebParser
Url=[MeasureWebsite]
StringIndex=1

[MeasureCloudBase]
Measure=Plugin
Plugin=WebParser
Url=[MeasureWebsite]
StringIndex=2

[MeterSkyCover]
MeasureName=MeasureSkycover1
Meter=STRING
MeterStyle=StyleMetar
Text=Clouds %1

[MeterCloudBase]
MeasureName=MeasureCloudbase
Meter=STRING
MeterStyle=StyleMetar
Text=Base %1
I'm not yet familiar enough with regex to figure out how to return a value multiple times or none. I also need it to return 4 values whether there are 4 XML tags or not, because otherwise the stringIndex values will be off. Unless there is a simpler way to do this. I have tried several different methods (I've been at this for hours...) but I won't clutter the post up trying to explain them and make myself look silly.

Any thoughts? Thanks.
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: XML Parsing

Post by Kaelri »

This is what lookahead assertions are for. In you case, you would use something like this:

Code: Select all

[Variables]
URL="http://abcd"
SkyCover=sky_cover="(.*)"
CloudBase=cloud_base_ft_agl="(.*)"
SkyCondition=(?(?=.*<sky_condition).*#SkyCover#.*#CloudBase#.*)

[MeasureWebsite]
Measure=Plugin
Plugin=WebParser
UpadateRate=1800
URL=#URL#
RegExp="(?siU)#SkyCondition##SkyCondition##SkyCondition##SkyCondition#"
You'll be glad to know that these are specifically designed to preserve the StringIndex order. :)
StallSpin
Posts: 5
Joined: December 11th, 2012, 6:13 am

Re: XML Parsing

Post by StallSpin »

Thanks a lot for that info, I finally got everything reading correctly.

I went on to spend 30 minutes trying to determine why the skin refused to update from my XML, only to find out that I had entered "UpadateRate"...