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];
}
}
}
Related
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
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
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;
}
}
}
I'm going to cut straight to the question, I have a XML file that has attributes like for example:
<Lowest units="p">135.9</Lowest>
What I need to get is the value from 'units', as if I just get the value from 'Lowest' it displays it as ' { Lowest = "135.9"\n\t; } ' and I'm assuming if I got it from the attribute it would just display '135.9' instead of above.
Here's the main two bits of coding I'm using:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:#"Fuel"])
{
item = [[NSMutableDictionary alloc] init];
fuel_price = [[NSMutableString alloc] init];
fuel_type = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(#"ended element: %#", elementName);
if ([elementName isEqualToString:#"Fuel"])
{
[item setObject:fuel_price forKey:#"p"];
[item setObject:fuel_type forKey:#"type"];
[prices addObject:[item copy]];
NSLog(#"adding fuel prices: %# - %#", fuel_price, fuel_type);
}
}
Hopefully someone can point me in the right direction, as I've tried so many methods with no luck.
Thanks in advance! :)
All the attributes are stored inside this attributeDict NSDcitionary you get in didStartElement... method and it's formatted so that attribute name is the key and value is, well, value :) So inside your element if you want to get the value of attribute named units you would do it this way:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"Lowest"]) {
NSString *units = [attributeDict objectForKey:#"units"];
}
}