how can I extract the title of a RSS channel while not getting in conflict with the title element of a news item?
If an element was found:
- (void)parser:(NSXMLParser *) parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
...
if ([elementName isEqualToString:#"channel") {
[currentChannel release];
currentChannel = nil;
currentChannel = [[NSMutableString alloc] init];
}
if ([elementName isEqualToString:#"item"]) {
...
}
}
If the end-tag was found:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"channel") {
[channel setObject:currentChannelTitle forKey:#"title"];
}
if ([elementName isEqualToString:#"item"]) {
...
}
Do the parsing stuff:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([currentElement isEqualToString:#"title") {
[currentChannelTitle appendString:string forKey:#"title"];
}
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
}
...
}
And in the last part I have the problem. I have two "title" attributes. One for the "channel" element and one for the child element of it ("item"). But I need a distinction somehow. But how?
i think you want to get item title, in that case in viewDidload method take one bool type variable say flag in
viewDidload() method set flag=NO;
when during parsing you find a start element as "Item" set flag = YES, and In parser didendElement method when you found element as "Title", check that flag=YES or not. store only if it is YES.
and also in ParserDidEndElement when you find element as "Item" again set flag=NO;
I found another example to support different categories of the feed and I come up with the following solution:
First declare the variable in the *.h file.
#interface XMLParser : NSObject <NSXMLParserDelegate>{
...
NSMutableString * currentChannel;
}
In the *.m file synthesize and release the variable. In didStartElement ask which element you are currently working on. If it is a channel element, extract the title and store it in the above declared variable.
#implementation XMLParser
...
#synthesize currentChannel;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
...
if ([elementName isEqualToString:#"item"]) {
...
}
// title is an attribute of the element channel
if ([currentElement isEqualToString:#"channel"]) {
[currentChannel appendString:[attributeDict objectForKey:#"title"]];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
...
if ([currentChannel isEqualToString:#"Your Channel Name"]) {
if ([currentCategory isEqualToString:#"Your category name"]) {
[items addObject:[item copy]];
}
} else {
[items addObject:[item copy]];
}
}
}
In didEndElement ask if the item belongs to a specific channel, and if then add it only if it is the defined category. So you can add/show only rss feeds, if they belong to a certain category.
So i've not tested it, because the channel only have one category. So I don't have to ask for the channel titel, to show only defined categories from a specific channel. But I think that should work.
Related
I am new in iphone Development, I don't know much about parsing. But I tried following code to get the distance element text. I am doing XMl parsing Of following link:
http://maps.googleapis.com/maps/api/directions/xml?origin=30.9165904,75.8634752&destination=30.89314000,75.86938000&sensor=true
Please check my code which i have tried:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:
(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if([currentElement isEqualToString:#"distance"])
{
NSLog(#"ENTER IN distance");
text = [[NSMutableString alloc]init];
{
dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:text,#"text",nil];
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"text"])
{
[dictionary setObject:text forKey:currentElement];
[text appendString:string];
NSLog(#"text....%#",text);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"distance"])
{
[distanparsingarray addObject:dictionary];
}
}
By using above method I am getting the text both of distance and duration. But I want only distance text. Please tell me what I am doing wrong in above code.
thanks in advance.
You have to manage it by below way.
Add on BOOL variable at class level. Set it when you identify currentElement as distance.
Inside found characters append string to text only when above BOOL is true.
Make this BOOL as false in didEndElement when currentElement is not equal to distance.
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:
(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if([currentElement isEqualToString:#"distance"])
{
flag = TRUE; //created at class level.
NSLog(#"ENTER IN distance");
text = [[NSMutableString alloc]init];
{
dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:text,#"text",nil];
}
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"text"] && flag == TRUE)
{
[dictionary setObject:text forKey:currentElement];
[text appendString:string];
NSLog(#"text....%#",text);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:
(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"distance"])
{
[distanparsingarray addObject:dictionary];
flag = FALSE;
}
}
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.
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,position,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 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.....
This is the xml I am getting from web:
<?xml version="1.0"?>
<abc>87C4A556-B7E5-5AE4-81FE-86BE0C6306E1</abc>
<abc2>P29077758</abc2>
<abc3>55AGD99D</abc3>
<abc4>147</abc4>
<abc5>1286259226</abc5>
<abc6>USA</abc6>
<abc7>US</abc7>
and using this to get attribute:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if( [elementName isEqualToString:#"a"])
{
recordResults = FALSE;
// greeting.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
Something like that, but I don't have any idea how can I get attribute from returned xml and assign those returned variables into my created variable. How can I do this?
http://blancer.com/tutorials/i-phone/76999/parsing-xml-files/ hope this helps !!!
I highly recommend using TouchXML to do your XML parsing.
I personally had all kinds of issues with NSXMLParser and ended up using TouchXML. Works a treat!
You should implement the following methods:-
One way you can use is :-
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
//check that the element is the one you want. If it is then initialize the string in which you want to store the value.
if(elementName isEqualToString:#"abc")
{
tempString = [[NSMutableString alloc] init];
}
}
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[tempString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if(elementName isEqualToString:#"abc")
{
self.StringToStore = tempString;
}
}