Trying to parse XML - ios

I am trying to parse the following XML File:
However when I parse it:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElementName = elementName;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElementName isEqualToString:#"link"]) {
NSLog(#"Link: %#",string);
}
}
I get the following NSLOG:
Link: http://www.flickr.com/photos/114014058#N06/
Link:
Link: http://www.flickr.com/photos/114014058#N06/
Link:
Link:
Link: http://www.flickr.com/photos/114014058#N06/11865861164/
Link:
As you see the URL I want is not appearing and there is a bunch of empty URLs.
Why?
Thank you!
EDIT:
I tried Rhythmic Fistman's answer but all I get is:
link is (null)

Try the didStartElement callback.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
When elementName is #"link" your url should be in attributeDict[#"href"].
e.g.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"link"]) {
NSLog(#"link is %#\n", attributeDict[#"href"]);
}
}

You'll find element starts in didStartElement, so change state there. When you get foundCharacters, check that you are in the right state, then the characters there are what you want.
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// assume currentElementName starts out nil. if we find "generator", enter
// "generator" state. if we're in "generator" state and find "link" then enter
// "link" state, which is our target state
if ([currentElementName isEqualToString:#"generator"] && [elementName isEqualToString:#"link"])
currentElementName = #"link";
// now we're in the tag we want to be in. get attributes from
// the attributeDict, if you need the tag body, check for "link" state in foundChars
NSLog(#"here are the attributes we want %#", attributeDict);
else if ([elementName isEqualToString:#"generator"]) {
currentElementName = #"generator";
}
}

Related

NSXMLParser ios - How to parse child nodes

I have an xml format, here's the part of it..
<a:IngredientItemIds xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
enter<b:int>206932</b:int><b:int>206930</b:int></a:IngredientItemIds><a:IsGiftCard>false</a:IsGiftCard>
..and so on..
My problem is that the XML parser fetches "none" for element "IngredientItemIds"
Instead what I wanted was an array of all "int" tags..
I am using the methods of NSXML parser foundcharacters, didstartElement and didEndElement
Does Any one know how to parse the child nodes in NSXMLparser ?
Thanks.
Try this:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
// If the current element name is equal to "a:IngredientItemId " then initialize the temporary dictionary.
if ([elementName isEqualToString:#"a:IngredientItemIds"]) {
self.dictTempDataStorage = [[NSMutableDictionary alloc]init];
}
// Keep the current element.
self.currentElement = elementName;
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"a:IngredientItemIds"]) {
[self.arrNeighboursData addObject:[[NSDictionary alloc] initWithDictionary:self.dictTempDataStorage]];
}
else if ([elementName isEqualToString:#"b:int"]){
// If the b:int element was found then store it.
[self.dictTempDataStorage setObject:[NSString stringWithString:self.foundValue] forKey:#"b:int"];
}
// Clear the mutable string.
[self.foundValue setString:#""];
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// Store the found characters if only we're interested in the current element.
if ([self.currentElement isEqualToString:#"b:int"] ) {
if (![string isEqualToString:#"\n"]) {
[self.foundValue appendString:string];
}
}
}

How Can I parse an xml with empty tags using NSXMLParser?

How to parse the xml tags with empty values to get null values using NSXMLParser. I'm using the following code :
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"Video"])
{
isStatus = YES;
}
if ([elementName isEqualToString:#"ArrayOfVideo"])
{
isStatus = NO;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementName isEqualToString:#"MediaOriginalFileName"])
{
[originalFileAry addObject:currentNodeContent];
}
if ([elementName isEqualToString:#"MediaType"])
{
[typeArray addObject:currentNodeContent];
}
if ([elementName isEqualToString:#"VideoMediaSource"])
{
[tableda addObject:currentNodeContent];
}
if ([elementName isEqualToString:#"VideoThumbnail"])
{
[tableImg addObject:currentNodeContent];
}
if ([elementName isEqualToString:#"VideoTitle"])
{
[tableData addObject:currentNodeContent];
}
}
}
here I'm not getting the empty values for the tag "VideoThumbnail".. Please help me to solve this...
this is what i tried ,and i found empty string, as expected from your example.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(#"the tag is %#",elementName);
if([elementName isEqualToString:#"VideoThumbnail"])
{
NSLog(#"video thumbnail tag arrived");
datastringThumbnail =[[NSMutableString alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(datastringThumbnail)
{
datastringThumbnail = [NSMutableString stringWithFormat:#"%#",string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
{
if([elementName isEqualToString:#"VideoThumbnail"])
NSLog(#"the string is : %#",datastringThumbnail);
// [tableImg addObject:currentNodeContent];
// currentNodeContent is datastringThumbnail in my case
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(#"the string we get is %#",datastringThumbnail);
}
FYI, datastringThumbnail is mutable string that will store you data of thumbnail tag(if any). As in your case there was no data in that tag, this string was empty. Use it wherever you want.
Actually, thing is that your method
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}
is not being called since there are no data inside that particular data. So in following method ,you can do as follow :
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementName isEqualToString:#"VideoThumbnail"])
{
if([currentContent Length]>0)
{
[tableImg addObject:currentNodeContent];
}
else
{
[tableImg addObject:#" "];
}
}
}
}
forgive my indentation... Also TBXML parsing is much faster and easier way to work with xml parsing. you can directly give names of tags and their child nodes.
You try in
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
May be you get the getting the empty values for the tag "VideoThumbnail".
Following link useful for you.
https://developer.apple.com/library/ios/samplecode/SeismicXML/Listings/SeismicXML_APLParseOperation_m.html#//apple_ref/doc/uid/DTS40007323-SeismicXML_APLParseOperation_m-DontLinkElementID_11

XML Parsing within TAG in ios

I have a XML file:
<trkseg>
<trkpt lat="11.004661" lon="76.97364"></trkpt>
<trkpt lat="11.005497" lon="76.97496"></trkpt>
</trkseg>
Here is my code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"trkseg"])
{
self.tag = [[MRTRouteMapTag alloc]init];
NSLog(#"SRC: %#",[attributeDict objectForKey:#"lat"]);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[self.workingPropertyString appendString:string];
}
//event_logo, event_name, event_venue, event_date_time, race_details, event_descriptions;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if (self.tag)
{
NSString *trimmedString = [self.workingPropertyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// clear the string for next time
[self.workingPropertyString setString:#""];
if ([elementName isEqualToString:#"trkseg"])
{
[self.array addObject:self.tag];
self.tag = nil;
NSLog(#"Array Count ==>>>>>> %d", [self.array count]);
}
// Get Registered User Details
// ------ ------- ------------
else if ([elementName isEqualToString:#"trkpt"])
{
NSDictionary *attributeDict;
NSString *imageURLString = [attributeDict objectForKey:#"lat"];
self.tag.latitude = trimmedString;
NSLog(#"race_id ==>> %#", imageURLString);
}
}
}
Here I need to parse data from trkpt TAG of lat and lon value. Could any one guide me to achieve this.. Thanks in Advance.
lat and lon are not values, but attributes of node trkpt
They should be available to you at didStartElement like so:
[attributeDict objectForKey:#"lat"];
or
[attributeDict objectForKey:#"lon"];
This is not related to NSXMLPArser, but the XML structure itself.
The values that you are looking for are stored in the attributeDict dictionary in
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
At finally i find the solution to archive it..
Here i added my sample code..
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"trkpt"])
{
self.tag = [[MRTRouteMapTag alloc]init];
lat = [attributeDict objectForKey:#"lat"];
lon = [attributeDict objectForKey:#"lon"];
tag.latitude = lat;
tag.longitude = lon;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[self.workingPropertyString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (self.tag)
{
if ([elementName isEqualToString:#"trkpt"])
{
[self.array addObject:self.tag];
self.tag = nil;
}
}
}

what test should be put to get nested element

Here is the case, title exist in two occurrences, before and after entry:
<feed xmlns:im="http://xxx.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<id>http://xxxxx/xml</id><title>xxxxx: Top articles</title><updated>2013-07-20T04:30:05-07:00</updated><link rel="alternate" type="text/html" href="https://xxxx;popId=1"/><link rel="self" href="http://xxxxxxx/xml"/><icon>http://xxxxxxxxxxx</icon><author><name>xxxxxxx</name><uri>http://www.xxxxx.com/xxxxx/</uri></author><rights>Copyright 2001</rights>
<entry>
<updated>2013-07-20T04:30:05-07:00</updated>
<id im:id="621507457">https://xxxxx.xxxx.com/us/album/blurred-lines-feat.-t.i.-pharrell/id621507456?i=621507457&uo=2</id>
//I want to get only the title nested in entry tree
<title>Robin Thicke</title>
What test should be made in didStartElement: protocol method in order to escape any outside the <entry></entry>?
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"title"]) {
//This is not enough, since it bring also the title value which is outside the entry tree
}
}
I don't want to rely on an external library like TBXML, I want to know wether this is doable in pure NSXMLParserDelegate?
You have to keep track of whether the parser is currently "inside" an entry element or not.
For example, you can
add an integer property entryLevel to the class,
set it initially to zero
and update it in didStartElement
and didEndElement:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"entry"]) {
self.entryLevel++;
} else if ([elementName isEqualToString:#"title"] && self.entryLevel > 0) {
// title inside entry
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"entry"]) {
self.entryLevel--;
}
}

how to parse my xml string using NSXmlParser?

hi i am new to iOS developement.
following is my response by web sevice in xml formart.how can i parse this string by using xml parser.
plz help me
<registration><status>3</status></registration>
thanks in advance
Using these method you can complete your xml parsing.Try These method.
-(NSMutableArray *)parseXML:(NSString *)data
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

Resources