iOS XMLParser - get url - ios

I would like to parse an XML tag that looks something like this:
<image href="..."/>
I'm currently using BlockRSSParser to do it.
I've tried to do it in the following method:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// String is empty:
}
What is the correct way to parse XML like this using XMLParser?

You get attribute values in the parser:didStartElement:namespaceURI:qualifiedName:attributes: method:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"image"]) {
NSString *href = attributes[#"href"];
}
}

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];
}
}
}

Retrieve image from XML using NSXMLParser

I have an RSS feed with the image of the feed encoded like this:
<imageItem src="http://www.example.com/picture.jpg">
<img width="386" height="173" src="http://www.abb-conversations.com/DACH/files/2015/06/Traumpraktikum-755-386x173.jpg" class="attachment-teaser-tablet wp-post-image" alt="Traumpraktikum-755" style="float:left; margin:0 15px 15px 0;"/>
</imageItem>
Using NSXMLParser, I was to be able to extract that src link. Currently the way I'm doing it does not return anything because it is looking for something like <imageItem> xxx.jpg </imageItem>.
Here is the code:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([self.element isEqualToString:#"title"]) {
[self.title appendString:string];
}
else if ([self.element isEqualToString:#"link"]) {
[self.link appendString:string];
}
else if ([self.element isEqualToString:#"imageItem"]) {
[self.image appendString:string];
}
}
Try this delegate method
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)nameSpaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:#"img"])
{
NSLog(#"SRC: %#",[attributeDict objectForKey:#"src"]);
}
}
The image url is set as the attribute of that img element.
You need to implement the following NSXMLParser delegate
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"img"])
{
NSString *urlValue = [attributeDict valueForKey:#"src"];
NSLog(#"%#",urlValue);
}
}
Refer NSXMLParserDelegate for more details

Trying to parse XML

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";
}
}

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

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