iOS Simple XML Parsing For Weather - ios

I have done xml parsing before using some guides from Ray Wenderlich, but those are usually for when there are multiple items involved. I am working on having an app simply display weather conditions at a given time, and the feed's XML just has conditions for time checked. Here is the feed in question:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Montgomery, AL</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Montgomery__AL/*http://weather.yahoo.com/forecast/USAL0375_f.html</link>
<description>Yahoo! Weather for Montgomery, AL</description>
<language>en-us</language>
<lastBuildDate>Thu, 03 Jan 2013 11:55 am CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Montgomery" region="AL" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="43" direction="0" speed="5" />
<yweather:atmosphere humidity="66" visibility="10" pressure="30.26" rising="2" />
<yweather:astronomy sunrise="6:46 am" sunset="4:52 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>
<item>
<title>Conditions for Montgomery, AL at 11:55 am CST</title>
<geo:lat>32.38</geo:lat>
<geo:long>-86.3</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Montgomery__AL/*http://weather.yahoo.com/forecast/USAL0375_f.html</link>
<pubDate>Thu, 03 Jan 2013 11:55 am CST</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="46" date="Thu, 03 Jan 2013 11:55 am CST" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 46 F<BR />
<BR /><b>Forecast:</b><BR />
Thu - Partly Cloudy. High: 50 Low: 31<br />
Fri - Sunny. High: 57 Low: 35<br />
<br />
Full Forecast at Yahoo! Weather<BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
<yweather:forecast day="Thu" date="3 Jan 2013" low="31" high="50" text="Partly Cloudy" code="30" />
<yweather:forecast day="Fri" date="4 Jan 2013" low="35" high="57" text="Sunny" code="32" />
<guid isPermaLink="false">USAL0375_2013_01_04_7_00_CST</guid>
</item>
</channel>
</rss>
Basically, all I need for the app is the current temperature and conditions, which under the item tag is contained within yweather:conditions tag. Using GDataXML, what would be simplest way of getting this info and setting an NSString equal to it?
Right now, I'm using some complex stuff from Ray's tutorial with asihttprequest and blocks for sorting and putting it all into an NSMutableArray, but I'm fairly certain it doesn't need be that complex now.

I like TBXML for straightforward parsing: http://www.tbxml.co.uk/TBXML/TBXML_Free.html .

Related

XML - "Invalid XML Document."

I am trying to update google contact.
When i hit http request with below xml body, i am getting error
"Invalid XML Document."
Below is my XML request.
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
<entry>\n
<category scheme=\"https://schemas.google.com/g/2005#kind\" term=\"https://schemas.google.com/contact/2008#contact\"/>\n
<title>test Sun 1:00pm Kinjal S</title>\n
<gd:name>\n
<gd:fullName>test Sun 1:00pm Kinjal S</gd:fullName>\n</gd:name>
<gd:phoneNumber label=\"Mobile\" primary=\"true\">+6598765432</gd:phoneNumber>
<gd:phoneNumber label=\"Father\">+6598732465</gd:phoneNumber>
<gd:phoneNumber label=\"Friend\">+6589898989</gd:phoneNumber>
<gContact:groupMembershipInfo deleted=\"false\" href=\"https://www.google.com/m8/feeds/groups/paresh.tps#gmail.com/base/6\"/>\n
</entry>"
Can someone help me to resolve this issue?
I believe the 2 namespaces gd and gContact is not defined. I just added the namespaces and it seems good. Here is the modified XML with dummy namespaces (Please correct it accordingly),
<?xml version="1.0" encoding="UTF-8"?>
<entry
xmlns:gd = "http://www.w3.org/TR/html4/"
xmlns:gContact = "http://www.w3.org/TR/html4/">
<category scheme="https://schemas.google.com/g/2005#kind" term="https://schemas.google.com/contact/2008#contact"/>
<title>test Sun 1:00pm Kinjal S</title>
<gd:name>
<gd:fullName>test Sun 1:00pm Kinjal S</gd:fullName>
</gd:name>
<gd:phoneNumber label="Mobile" primary="true">+6598765432</gd:phoneNumber>
<gd:phoneNumber label="Father">+6598732465</gd:phoneNumber>
<gd:phoneNumber label="Friend">+6589898989</gd:phoneNumber>
<gContact:groupMembershipInfo deleted="false" href="https://www.google.com/m8/feeds/groups/paresh.tps#gmail.com/base/6"/>
</entry>

Podcast from my server works on iTunes desktop, but not on mobile

I have an issue where I'm trying to host a podcast via RSS feed myself, and I'm running into a strange issue. I validated my RSS feed and all of that, got everything working on iTunes on the desktop just fine. However, when I go to play that same podcast on my iPhone, I either get a "podcast is temporarily unavailable" error, or nothing at all. All of the information shows up fine and everything, but nothing will play. I made sure to check with my hosting service that Byte Range Requests are supported, and enabled. I don't know what the problem could be. The RSS looks like this (I edited some personal information out):
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Striate Cortex Testing...</title>
<description>This is a test of the Striate Cortex Podcasting System...</description>
<link>http://www.striatecortex.com</link>
<language>en-us</language>
<copyright>Copyright 2016</copyright>
<lastBuildDate>Tue, 16 Feb 2016 08:55:00 -0500</lastBuildDate>
<pubDate>Tue, 16 Feb 2016 08:55:00 -0500</pubDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<webMaster>EMAIL (NAME)</webMaster>
<itunes:author>Striate Cortex</itunes:author>
<itunes:subtitle>PODCASTNAME is a show about interviewing other musicians, and talking about what life is like on the road.</itunes:subtitle>
<itunes:summary>PODCASTNAME is a show about interviewing other musicians, and talking about what life is like on the road. Something else to make this longer.</itunes:summary>
<itunes:owner>
<itunes:name>Striate Cortex</itunes:name>
<itunes:email>EMAIL</itunes:email>
</itunes:owner>
<itunes:explicit>Yes</itunes:explicit>
<itunes:image href="http://www.striatecortex.com/podcast.png"/>
<itunes:category text="Music">
</itunes:category>
<atom:link href="http://www.striatecortex.com/test.xml" rel="self" type="application/rss+xml" />
<item>
<title>Striate Cortex - PodcastName Test # 1</title>
<link>http://www.striatecortex.com/podcast/test</link>
<guid>http://www.striatecortex.com/test.mp3</guid>
<description>This is a description.</description>
<enclosure url="http://www.striatecortex.com/test.mp3" length="29184000" type="audio/mpeg"/>
<category>Podcasts</category>
<pubDate>Tue, 16 Feb 2016 08:55:00 -0500</pubDate>
</item>
</channel>
</rss>

How to get data from this kind of rss on ios

How to get data from this kind of rss on ios ?
how to get data inside the <![CDATA[ ?
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Title 1</title>
<link>http://link1.com</link>
<description>Description 1</description>
<language>en-us</language>
<pubDate>Wed, 27 Feb 2013 12:38:54 EST</pubDate>
<lastBuildDate>Wed, 27 Feb 2013 12:38:54 EST</lastBuildDate>
<webMaster>webmaster#site.com (Hunny Bunny)</webMaster>
<item>
<title>item 1 title</title>
<link>http://item1 link</link>
<description><![CDATA[
<h2>My Item 2</h2>
<img src="http://itemi.com/image1.png" align="left"/>
]]></description>
<pubDate>Wed, 27 Feb 2013 12:10:40 EST</pubDate>
</item>
<item>
<title>item 2 title</title>
<link>http://item2.com/link</link>
<description><![CDATA[
<h2>My Item 1</h2>
<img src="http://itemi.com/image1.png" align="left"/>
]]></description>
<pubDate>Wed, 27 Feb 2013 12:10:40 EST</pubDate>
</item>
</channel>
</rss>
An option would be to use the NSXMLParser. I've used it with great success when parsing my RSS as well as ATOM feeds.
To examine information within <![CDATA[...]]>, you'll want to implement the following method of the NSXMLParserDelegate protocol:
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
Here's a terrific tutorial to get you started: Parsing XML data with NSXMLParser

Understand xml parsing in Apple sample code

I'm studying Apple's LazyTableImages sample code. I'd like to understand how the app is pulling data from the RSS feed included in the app:
http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/xml
How are the contents at the above url parsed? Viewing the page source reveals HTML with no apparent xml section. While looking through the sample parsing code I found a few symbols like im:name. However these symbols are not in the contents of the above url.
I tried to host the contents of the above url locally (w/ limit=1). However pointing the sample code to #"~/Desktop/a.xml" causes the application to throw the error unsupported url.
More info: While reading http://en.wikipedia.org/wiki/Rss I came across what I expected to see at phobos link above. Something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Title</title>
<description>This is an example of an RSS feed</description>
<link>http://www.someexamplerssdomain.com/main.html</link>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
<item>
<title>Example entry</title>
<description>an interesting description</description>
<link>http://www.wikipedia.org/</link>
<guid>unique string per item</guid>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
</item>
</channel>
</rss>
Is there an equivalent "human-readable" xml document corresponding to the above phobos link somewhere?
You're right, the feed you're looking at technically isn't an RSS Feed. It's an Atom 1.0 Feed, but both are popular XML-based feed formats.
If you view the source of the feed you will see the XML elements you're looking for, like:
<entry>
<updated>2011-12-09T16:15:32-07:00</updated>
<id>http://itunes.apple.com/us/app/tetris/id479943969?mt=8&uo=2</id>
<title>TETRISĀ® - Electronic Arts</title>
<summary>Long summary here</summary>
<im:name>TETRISĀ®</im:name>
...
</entry>
Some browser versions parse RSS/Atom feeds into user-friendly HTML pages and present them instead of the actual feed, it sounds like that's the type of HTML page you're viewing.
On a OS X, you could use a command like Curl to download the feed in a Terminal:
curl -o feed.xml http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/xml

Finding a text node in xml using xpath problem

I'm using rails and the Nokogiri parser. My xml is as below and I'm trying to get the 'Biology: 08:00' text into my view.
<rss version="2.0">
<channel>
<item>
<title>Biology: 08:00</title>
<description>Start time of Biology</description>
<pubDate>Tue, 13 Oct 2009 UT</pubDate>
</item>
</channel>
</rss>
I can find the node with the text 'biology' using the code below
#content = doc.xpath('//title[contains(text(),"Biology")]')
When I move it into my view it strangely ends up as the title of my .html.erb page. I can't seem to get it into the body with
<body>
<%=#content%>
</body>
anyone know what's going on?
You're getting the whole node, and the node is a <title> tag.
you want:
#content = doc.xpath('//title[contains(text(),"Biology")]/text()')
to get the text content of the node

Resources