I have an Application which contains only WebView, i need to parse a XML file and display it in the WebView . How can i do that ?
Follow this , How to parse XML in Objective c using ASIHTTPRequest and handle all this methods
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
See this Link too
This all are beginners guide...
Follow Marvin's suggestion to parse xml,
now if you want to display string on webview, then you can use-
[myWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.abc.com"]];
Follow links for XML parsing tutorials:
[NSXML parser tutorial]: http://iphonebyradix.blogspot.com/2011/08/parsing-xml-using-nsxmlparser.html
[GDataXML parser tutorial]: http://iphonebyradix.blogspot.com/2011/03/using-gdata-to-parse-xml-file.html
Related
I have the following xml file which I am parsing using NSXMLParser :
<geometry id="window_strip-mesh" name="window_strip">
<mesh>
<source id="window_strip-mesh-positions">
<float_array id="window_strip-mesh-positions-array" count="1302">399.297 -87842.3 233.334 399.297 -89320.4 233.334 -821.159
...
</float_array>
My question is how can I detect / capture the values that are listed after the > (i.e the 399.297 -87842.3 and so on). Is NSXML Parser able to pick these up ?
Thanks !
you can implement the NSXMLDelegate methods,
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock;
and
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
For your example you have to put the following line in the didStartElement: Method:
if([elementName isEqualToString:#"float_array"]) {
float_array_bool = YES;
}
Then in the - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string; Method you can implement
if (float_array_bool) {
[yourItemToStoreValues.floatArray addObject:string];
}
In the parserDidEndElement: Method you have to unset tho bool value:
if([elementName isEqualToString:#"float_array"]) {
float_array_bool = NO;
}
That should do the trick
How do you extract attributes from XML using NSXML parser??
Heres my xml:
<item>
<title>Button hails 'amazing' win</title>
<description>Jenson Button hailed yesterday's crazy Canadian Grand Prix victory as the best of his Formula One career.
</description>
<link>http://www.skysports.com/story/0,19528,12433_6986809,00.html</link>
<guid isPermaLink="false">12433_6986809</guid>
<pubDate>Mon, 13 Jun 2011 06:21:00 GMT</pubDate>
<category>News Story</category>
<enclosure type="image/jpg" url="http://img.skysports.com/11/06/128x67/Canadian-GP-Jenson-Button-celebrates1_2609288.jpg" length="123456" />
</item>
I need to get the url from the enclosure tags.
Thanks
if([elementName isEqualToString:#"enclosure"])
{
NSString *urlValue=[attributeDict valueForKey:#"url"];
NSString *urlValue=[attributeDict valueForKey:#"type"];
NSString *urlValue=[attributeDict valueForKey:#"length"];
}
you need to use NSXMLParser and its delegate functions
-
(BOOL) parse:(NSData *)xmlData
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
then you need to use some thing like this
if([elementName isEqualToString:#"enclosure"])
{
NSMutableDictionary *Dict=[NSMutableDictionary dictionary];
[Dict setObject:[attributeDict valueForKey:#"url"] forKey:#"url"];
[categoryDict setObject:[attributeDict valueForKey:#"type"] forKey:#"type"];
}
The method...
(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
Gives you a dictionary of attributes and their keys (attributeDict)... look for an entry keyed "url" when the elementName is equal to "enclosure"...
Here is explanation:
Example xml with attribute:
Use xml delegate method:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"remoteContent"]){
NSString* href = [attributeDict objectForKey:#"href"];
NSLog(#"href %#",href);
[someArray addObject:href];
}
}
I am calling the saop web service from the iPhone app. The soap response is a byte []. How can I parse it in the iPhone and display the data in text view? Please help me out.
you have to parse that data friend using nsxmlparserdelegate.
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName { }
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ }
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ }
-(void)parserDidEndDocument:(NSXMLParser *)parser{ }
I need to parse the rss feed from feed://feeds.huffingtonpost.com/huffingtonpost/LatestNews.
But, I could not read Link tag of data because it doesn't have separate start and end tag.
<link id="apple-rss-icon" rel="icon" href="file://localhost/System/Library/Frameworks/PubSub.framework/Versions/A/Resources/PubSubAgent.app/Contents/Resources/favicon.tif" type="image/x-icon" />
I am using NSXMLParser class with following methods.
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
(void)parserDidStartDocument:(NSXMLParser *)parser
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
Non-paired tags such as what you're showing will still fire NSXMLParser events and you can access the element's attributes inside the didStartElement method.
For an example of a pretty full-featured Objective-C RSS parser, check out MWFeedParser.
There are a couple of rss parser implementations available.
E.g https://github.com/mwaterfall/MWFeedParser
Maybe you can use this or get some inspiration from it.
i can only parse 1st element of this xml but i want to get all the content of cuisine
this is my xml
<cuisines>
<cuisine>Asian</cuisine>
<cuisine>Nepalese</cuisine>
<cuisine>North Indian</cuisine>
<cuisine>Indian Vegetarian</cuisine>
<cuisine>Organic Cuisine</cuisine>
</cuisines>
and this is the code to parse
if ( [elementName isEqualToString:#"cuisines"]) {
if ( [elementName isEqualToString:#"cuisine"] ) {
elementName=[[NSMutableString alloc] init];
keyInProgress = [elementName copy];
// This is a string we will append to as the text arrives
textInProgress = [[[NSMutableString alloc] init]autorelease];
return;
}
but its not working i want to get all the value
you need to implement the parser method as well as the following NSXMLParserDelegate protocol methods methods:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
A good tutorial can be found here: Consuming a RESTful Service (bit.ly) in an iPhone Application