How to parse the second child node from xml page in iphone - iphone

I am new to iphone development.I want parse an you-tube XML page and retrieve its contents and display in a RSS feed.
my xml page is
<entry>
<id>xxxxx</id>
<title>xxx xxxx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
<media:thumbnail url="http://tiger.jpg"/>
</media:group>
</entry>
To retrieve the content i am using xml parsing.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"entry"]) {
entry = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentcontent = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"entry"]) {
[entry setObject:currentTitle forKey:#"title"];
[entry setObject:currentDate forKey:#"content"];
[stories addObject:[entry copy]];
}}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"content"]) {
[currentLink appendString:string];
}
}
I am able to retrieve id , title and content value and display it in a table-view.How can i retrieve tiger image URL and display it in table-view.Please help me out.Thanks.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"media:thumbnail"])
imageUrl=[attributeDict objectForKey:#"url"];
}
Since you already have an if statement in this method, use else if for this one

Related

How to make a smooth XML Parsing for Tags & sub tags within it...?

I have to parse number of tags & sub tags also in XML format, then how it can be possible to make it smooth at the time of parsing & fetching data to display?
Please suggest the proper way of getting it parsed.
//USE NSXMLParser
NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:#"YOUR_URL"]];
[parser setDelegate:self];
[parser parse];
// Below are the delegates which will get you the data
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:#"forecast_conditions"]){
getData = YES; // getData is a bool which is NO initialy
}
if(getData)
{
if([elementName isEqualToString:#"day_of_week"])
NSLog(#"%#",[attributeDict objectForKey:#"data"]);
if([elementName isEqualToString:#"low"])
NSLog(#"%#",[attributeDict objectForKey:#"data"]);
if([elementName isEqualToString:#"high"])
NSLog(#"%#",[attributeDict objectForKey:#"data"]);
if([elementName isEqualToString:#"icon"])
NSLog(#"%#",[attributeDict objectForKey:#"data"]);
if([elementName isEqualToString:#"condition"])
NSLog(#"%#",[attributeDict objectForKey:#"data"]);
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:#"forecast_conditions"]){
getData = NO;
}
}

iphone: NSMutable array is empty when using addObject in Xcode 4.3.2

I am parsing data using NSXMLParser.
I am able to get the parsed data. But when I add it to the NSMutableArray using addObject, the array is empty.
I am using the latest Xcode version.
Can someone please help me out.
The array I am working on is identificationTypes1
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"Books"]) {
}
if ([elementName isEqualToString:#"Book"]) {
if ([elementName isEqualToString:#"title"]) {
self.identificationTypes1 =[[NSMutableArray alloc]init];
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (!currentElementValue) {
currentElementValue =[[NSMutableString alloc]initWithString:string];
}
else {
[currentElementValue appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"Books"]) {
NSLog(#"Count is %#",[identificationTypes1 count]);
return;
}
if ([elementName isEqualToString:#"Book"]) {
}
if ([elementName isEqualToString:#"title"]) {
NSLog(#"Curre value is %#",currentElementValue);
[self.identificationTypes1 addObject:currentElementValue];
}
currentElementValue = nil;
}
NSMutabeArray *myArray = [[NSMUtableArray alloc]init];
[myArray addObject:<XML Data>];
You cannot nest elements as you have in didStartElement. Parsers do not allow that.
Take out the title out on its own and retest. You need to make new variables that are accessed conditionally.

how to differentiate two more Xml tags its containing same name in iPhone using ns xml parser

i'm new to iPhone development. i have two xml tags has a same name. how to differentiate this two tags. i mean how to identify when first tag will be closed
<data>
<level>0</level>
<type>video</type>
<file>file_CTcE96nTfP3sXIQ8.wmv</file>
<position>0,0,640,360</position>
<duration>121</duration>
</data>
<data>
<level>0</level>
<type>image</type>
<file>file_CTNJ1da7ntcTGmxd.jpg</file>
<position>640,0,1024,360</position>
<duration>121</duration>
</data>
<data>
<level>0</level>
<type>image</type>
<file>file_CTAo1yLWaZqXkpic.png</file>
<position>0,360,337,480</position>
<duration>121</duration>
</data>
i have implement these methods.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"data"]) {
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
???
}
here is there three data tags there. i want to put all each data tags in three array. how can i achieve that.i mean like this
data1(file,type,position,duration)
data2(file,type,position,duration)
data3(file,type,p‌​osition,duration)
In didStartElement
if ([elementName isEqualToString:#"data"]) {
data = [[NSMutableDictionary alloc] init];
level = [NSMutableString alloc] init];
type = [NSMutableString alloc] init];
position = [NSMutableString alloc] init];
duration = [NSMutableString alloc] init];
}
Then in didEndElement
if ([elementName isEqualToString:#"data"]) {
[data setObject:type forKey:#"type"];
[data setObject:position forKey:#"position"];
[data setObject:duration forKey:#"duration"];
[dataList addObject:data];
}
U need to add foundCharacters delegate method and assign values to strings here
if ([currentElement isEqualToString:#"type"]) {
[type appendString:string];
}
if ([currentElement isEqualToString:#"position"]) {
[position appendString:string];
}
if ([currentElement isEqualToString:#"duration"]) {
[duration appendString:string];
}
level, position and duration are to be parsed as normal tags..
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"data"])
{
}
}

How to parse the xml using nsxmlparser with same names of multiple nodes in iphone programming?

How can I parse using NSXMLParser if I need all the images,
<title>Title</title>
<description>my description is here </description>
<images>
<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.7218.jpg</image>
<image>http://www.sosmoths.com/mothImages/800px-Tineola.bisselliella.mounted.jpg</image>
<image>http://www.sosmoths.com/mothImages/800px-XN_Tineola_bisselliella_1.jpg</image>
<image>http://www.sosmoths.com/mothImages/Tineola_bisselliella.JPG</image>
</images>
There are more nodes like title, description, so how to parse such xml, I am confused about image nodes, I am thinking to use NSMutableArray for this, but still not clear to do code?
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:"<image>"])
{
[imagearray addObject:imageurl];
}
}
Before this you need to find the element name is and then do above code.This may help you.
use this it work
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
NSLog(#"%#",elementName);
if([elementName isEqualToString:#"moths"]){
mytblarray=[[NSMutableArray alloc] init];
} else if([elementName isEqualToString:#"moth"]){
tmpdic=[[NSMutableDictionary alloc] init];
}
else if([elementName isEqualToString:#"images"]){
imgArray=[[NSMutableArray alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(tmpstr!=nil && [tmpstr retainCount]>0){ [tmpstr release]; tmpstr=nil; }
tmpstr=[[NSString alloc] initWithString:string];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:#"moth"]){
[mytblarray addObject:tmpdic];
[tmpdic release];
}if([elementName isEqualToString:#"images"]){
[tmpdic setValue:imgArray forKey:elementName];
}
else if([elementName isEqualToString:#"image"]){
[imgArray addObject:tmpstr];
}
}
take dictionary in initWithData: methode
Take values as key like title, description
in didStartElement method, in images tag alloc image_array
in didEndElement method, in image tag add your data(here your image link) to image_array
now in didEndElement method, in images tag add that array to your main dictionary as key images
that's all.....

need help to parse xml string

i need to parse this xml file
<?xml version="1.0" encoding="UTF-8"?>
<imageset>
<category>
<image>SQUIRREL</image>
<image>FOX</image>
<image>TIGER</image>
<image>LION</image>
</category>
</imageset>
i use this code to parse this.
- (void)viewDidLoad {
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Config.xml"]]];
[super viewDidLoad];
[parser setDelegate:self];
[parser parse];
NSLog(#"test.......... %#",test);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"image"]) {
test = [[NSMutableArray alloc]init];
addelements = TRUE;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"image"]) {
addelements = FALSE;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[test addObject:string];
}
But it adds only last object,displays like this in console.
test.......... (
LION,
"\n\n",
"\n"
)
I need to add all image names in the xml file means {SQUIRREL,FOX,TIGER,LION}
how can i done,can any one please help me.
Thank u in advance.
Your problem is this method:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"image"]) {
test = [[NSMutableArray alloc]init];
addelements = TRUE;
}
}
Every time an image tag starts, you overwrite the test variable with a new instance of NSMutableArray (leaking the previous object). You need to change it to something like this:
if (!test) test = [[NSMutableArray alloc]init];
That is: only allocate a new array if there wasn't an old one already.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
[mutableString release];
mutableString = [[NSMutableString alloc] init];
if ([elementName isEqualToString:#"image"]) {
if( !test ) test = [[NSMutableArray alloc] initWithCapacity: 10];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"image"])
[test addObject: mutableString];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[mutableString appendString: string];
}
where test and mutableString are ivars declared in your view controller's header
Note: this is very simplified version.
#Mahesh Babu yes you can also add the image by giving the name only as for example......
[UIImage imageNamed:#"SQUIRREL.jpg"];