2015년 1월 3일 토요일

How to parse duplicated tag?

I have a big problem to parse Yahoo Weather XML.
I want to parse forecast block, but in this piece, XML have a duplicated tag, as shown below.
How to work around this problem?

<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/33.gif"/><br /> <b>Current Conditions:</b><br /> Fair, 5 C<BR /> <BR /><b>Forecast:</b><BR /> Fri - Partly Cloudy. High: 8 Low: 3<br /> Sat - Mostly Sunny. High: 10 Low: 6<br /> Sun - Mostly Sunny. High: 9 Low: 3<br /> Mon - Sunny. High: 8 Low: 0<br /> Tue -
]]>
<![CDATA[
Sunny. High: 8 Low: 1<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Rimini__IT/*http://weather.yahoo.com/forecast/ITXX0148_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]>
</description>

<yweather:forecast day="Fri" date="2 Jan 2015" low="3" high="8" text="Partly Cloudy" code="29"/>

<yweather:forecast day="Sat" date="3 Jan 2015" low="6" high="10" text="Mostly Sunny" code="34"/>

<yweather:forecast day="Sun" date="4 Jan 2015" low="3" high="9" text="Mostly Sunny" code="34"/>

<yweather:forecast day="Mon" date="5 Jan 2015" low="0" high="8" text="Sunny" code="32"/>

<yweather:forecast day="Tue" date="6 Jan 2015" low="1" high="8" text="Sunny" code="32"/>

<guid isPermaLink="false">ITXX0148_2015_01_06_7_00_CET</guid>



I don't see duplicate tags in what you posted... I see a quick forecast in the first data set, and a full forecast in the second data set...



Quick forecast is not good for my use.
See full forecast, the tag is...?
i think "yweather:forecast" for every day of the week, if you parse including the day, is not good, because we have only five days.



That's called a 5 day forecast. 



Yes is 5 days... and the problem is duplicated Tag, Not 5 days. How to distinguish one day?



your XML is not the complete result of the API, is it?
I would convert the XML result into a list of lists using theXMLTextDecode block and then use list blocks like select list itemand lookup in pairs to get what you need
you also can loop through similar data using the for each in list loop



any XMLTextDecode(text XmlText)
Decodes the given XML string to produce a list structure. See the App Inventor documentation on "Other topics, notes, and details" for information.




Also see

for the duplication problem.



Yes is only the part with the problem, is a XML response of Yahoo Weather webservice (http://weather.yahooapis.com/forecastrss?w=721682&u=c).
Now try with XMLtextdecode and lookup in pairs, it's ok with WOEID extract procedure, but take error with forecast --> org.json.XMLTokener.end
Try with JSON format and it's ok, but... take error with this part, the error is "not well formatted pairs". I'm very frustrating....

forecast
0
1
2
3
4



I don't know if you've seen this yet.
Here's an entirely different approach, that throws away
all hopes of parsing the entire tree ...


That was designed to just search for one target  based on its delimiters.

Extend that procedure into a new procedure, called Multi_Parse,
which would return a list of all such targets in its input and those delimiters.

I'm guessing your target is a single day forecast?

Parsing a single day forecast should be a separate post, if you need it.


댓글 없음:

댓글 쓰기