tricky XML

hi all
I am loading some remote XML info from yahoo weather into a string and then loading it into the ofxXMLSettings addon but I can’t get at the info i’m after…

  
  
<item>  
            <title>Conditions for Sydney, AS at 2:08 pm AEST</title>  
            <geo:lat>-33.96</geo:lat>  
            <geo:long>151.01</geo:long>  
            <link>[http://us.rd.yahoo.com/dailynews/rss/weather/Sydney--AS/*http://weather.yahoo.com/forecast/ASXX0112-f.html</link>](http://us.rd.yahoo.com/dailynews/rss/weather/Sydney--AS/*http://weather.yahoo.com/forecast/ASXX0112-f.html</link>)  
            <pubDate>Wed, 22 Oct 2008 2:08 pm AEST</pubDate>  
            <yweather:condition text="Light Rain Shower/Windy" code="11" temp="52" date="Wed, 22 Oct 2008 2:08 pm AEST" />  
            <description>  
                <![CDATA[--
<img-src="[http://l.yimg.com/us.yimg.com/i/us/we/52/11.gif"/><br](http://l.yimg.com/us.yimg.com/i/us/we/52/11.gif"/><br) />  
<b>Current Conditions:</b><br />  
Light Rain Shower/Windy, 52 F<BR />  
<BR /><b>Forecast:</b><BR />  
Wed - Showers. High: 58 Low: 53<br />  
Thu - Showers. High: 61 Low: 54<br />  
<br />  
<a href="[http://us.rd.yahoo.com/dailynews/rss/weather/Sydney--AS/*http://weather.yahoo.com/forecast/ASXX0112-f.html">Full](http://us.rd.yahoo.com/dailynews/rss/weather/Sydney--AS/*http://weather.yahoo.com/forecast/ASXX0112-f.html">Full) Forecast at Yahoo! Weather</a><BR/>  
(provided by The Weather Channel)<br/>  
]]>  
</description>  
            <yweather:forecast day="Wed" date="22 Oct 2008" low="53" high="58" text="Showers" code="11" />  
            <yweather:forecast day="Thu" date="23 Oct 2008" low="54" high="61" text="Showers" code="11" />  
            <guid isPermaLink="false">ASXX0112_2008_10_22_14_08_AEST</guid>  
        </item>  
  

i want the temperature (52 in this case), which is located inside the yweather:condition tag, or in a block of html inside the description tag.

The addon can’t access the first, and the second requires parsing the html. Is it possible to use poco to parse the XML better, or is there a simple regular expression class to parse the html in the description field?

any advice appreciated.
nay.

not terribly eloquent, but at the moment I’m using poco’s reg exp class to filter the temperature out of the description tag contents:

  
  
	RegularExpression reg_exp(", .* F");  
	string returnString;  
	int returnMatch = reg_exp.extract(xml_string, returnString);   
	string numString(returnString,2,2);  
	int temp = atoi(numString.c_str());   
  

Yeah unfortunately ofxXmlSetting can’t read attributes.
It is not meant to be a full xml parser but a basic one for loading and saving settings files.

Poco has a much more comprehensive xml parser - at some-point we plan to wrap it to make an ofxXML which would be a lot more comprehensive.

theo

Hey sometime ago I extended ofxXmlSettings to read from strings (instead of files) and read attributes.

the code is here

http://www.jesusgollonet.com/openFramew-…-er-src.zip

It is the class ofxXmlExtended and it just adds those 2 things. In that version everything is in the .h file, but works without problems.

Hope it helps.
jesús.

p.s: at that time I also tried to use teh poco xml parser and it sucked… or maybe it was me. anyway I prefer tinyxml by far.

cheers

huh wow!
that is cool!

if we could add a setAttribute function too then I think it would be a good idea to integrate your changes into the ofxXmlSettings addon.

Read from string is super useful too!

Theo

great, I’ll have a look at setAttribute this weekend and let you know.

cheers.
jesús.

Hi Jesus

Have been trying out ofxXmlExtended, seems great so far. Does getValue work for you?

I find that it does give me the right value when I pass in an xml string, but when that string is updated (via web) the getValue always returns the value the very first time you call getValue.

However this solves it by clearing the tag contents first…
xmlExtended.clear();
bool ok = xmlExtended.loadFromString(tstring);

hey chris,

getValue should work as in the original ofxXmlSettings, as I don’t overwrite anything there.

I must say I haven’t tried to update the contents of an xml on the fly though… Have you tried to do the same loading from file? Not sure, but I think it should do the same (ie: not overwriting the first contents unless you call clear). I don’t do anything different from the original xmlSettings

I would say that what you propose makes sense: as you want to overwrite the original object, you have to explicitly clear() it…

I might be totally wrong. Theo?

cheers

thanks heaps jesus
sorry for the late reply but my email notifications don’t seem to be working

accessing attributes would be great but i’m having some syntax problems though, i *think* it might be because of the colon in the tag name:

  
<yweather:condition  text="Fair"  code="34"  temp="81"  date="Mon, 27 Oct 2008 8:30 am AEST" />  

i’ve tried the following syntax without success:

  
string att = XML_out.getAttribute("rss:channel:item:yweather:condition", "temp", "0");  

and

  
XML_out.pushTag("rss");  
	XML_out.pushTag("channel");  
	XML_out.pushTag("item");  
	string att = XML_out.getAttribute("yweather:condition", "temp", "0");  
	XML_out.popTag();  
	XML_out.popTag();  
	XML_out.popTag();  

tried treating yweather and condition as separate too but the program always hangs.
any advice?

hey nay.

yes, looks like the colon might be the guilty one.

I don’t have time to test now, but you can try to change the tag delimiter in readAttribute. look for tokenize in readAttribute

  
  
bool readAttribute(...) {  
       vector<string> tokens = tokenize(tag,":");  
       ...  
       ...  
}  
  

and change it to something else like tokenize(tag, “#”) or whatever that doesn’t appear in the original tags.

then make sure that you use the new delimiter in your getAttribute calls.

  
  
XML_out.getAttribute("rss#channel#item#yweather:condition", "temp", "0");  
  

anyway, looks like it might be worth to make the delimiter an optional parameter, as it is also hardcoded in ofxXmlSettings

hope it helps,
jesús.

that would definitely be a problem - but also the fact that it closes it self with />
I think I remember tiny xml not liking that

theo

thanks! changed the delimiter to # and it now works despite the self closing tag:

  
string att = XML_out.getAttribute("rss#channel#item#yweather:condition", "temp", "0");