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
Related
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"];
}
}
whenever i try to request my server through NSUrlRequest, an xml code comes in -didRceivedata delegate method. when i print it some body fault come. How to do this?
With use of NSURLRequest, You may o/p as below
output code/somethng.
To get proper output ,you have to use - NSXMLParserDelegate and its delegate methods as below
-(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
I hope, you will get your answer...
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 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
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--;
}
}