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;
}
}
}
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'm working on an application where I use XML parsing in UITableView. I can not figure out how I'm separately parses attributes rok1 and rok2. If I use [_element isEqualToString: # "jmeno"] application works well, but it displayed to me in UITableView together rok1 and rok2 and do not want it. Thx for your help.
There is my parser code:
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
self.element = elementName;
if ([_element isEqualToString:#"rok1"])
{
_item = [[NSMutableDictionary alloc] init];
self.nazev = [[NSMutableString alloc] init];
self.definice = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([self.element isEqualToString:#"nazev"])
{
[self.nazev appendString:string];
}
else if ([self.element isEqualToString:#"def"])
{
[self.definice appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"rok1"])
{
Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
definice:self.definice];
[self.svatkyArray addObject:thisSvatek];
}
self.element = nil;
}
And example of my XML:
<?xml version="1.0" encoding="UTF-8" ?>
<menu>
<rok1>
<jmeno>
<nazev>Prvni</nazev>
<def>blblbla</def>
<jmeno>
<nazev>Druhy</nazev>
<def>blbablabal</def>
</jmeno>
</rok1>
<rok2>
<jmeno>
<nazev>Prvni</nazev>
<def>blblbla</def>
</jmeno>
</rok2>
</menu>
You need to keep track of your "state" as you are parsing the XML document. One simple approach is simply to add a boolean property that indicates that you are processing 'inside' the target element.
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
self.element = elementName;
if ([elementName isEqualToString:#"rok1"])
{
self.foundTarget=YES;
self.nazev = [[NSMutableString alloc] init];
self.definice = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (self.foundTarget) {
if ([self.element isEqualToString:#"nazev"])
{
[self.nazev appendString:string];
}
else if ([self.element isEqualToString:#"def"])
{
[self.definice appendString:string];
}
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"rok1"])
{
Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
definice:self.definice];
[self.svatkyArray addObject:thisSvatek];
self.foundTarget=NO;
}
self.element = nil;
}
A more sophisticated approach is to build some sort of data structure, such as a dictionary, that represents your XML
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
self.element = elementName;
if ([elementName isEqualToString:#"rok1"] || [elementName.isEqualToString:#"rok2"])
{
self.nazev = [[NSMutableString alloc] init];
self.definice = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([self.element isEqualToString:#"nazev"])
{
[self.nazev appendString:string];
}
else if ([self.element isEqualToString:#"def"])
{
[self.definice appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"rok1"] || [elementName.isEqualToString:#"rok2"])
Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
definice:self.definice];
self.rokDictionary[elementName]=thisSvatek; // self.rokDictionary is an NSMutableDictionary
}
self.element = nil;
}
To separate out the various 'jmeno' tags, you need to handle them in didStartElement & didEndElement -
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
self.element = elementName;
if ([elementName isEqualToString:#"rok1"] || [elementName.isEqualToString:#"rok2"])
{
self.jmenoArray=[NSMutableArray new];
}
else if ([elementName isEqualToString:#"jmeno"]) {
self.nazev = [[NSMutableString alloc] init];
self.definice = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([self.element isEqualToString:#"nazev"])
{
[self.nazev appendString:string];
}
else if ([self.element isEqualToString:#"def"])
{
[self.definice appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"jmeno"]) {
Slova *thisSvatek = [[Slova alloc] initWithName:self.nazev
definice:self.definice];
[self.jemnoArray addObject:thisSvatek];
}
else if ([elementName isEqualToString:#"rok1"] || [elementName.isEqualToString:#"rok2"])
self.rokDictionary[elementName]=self.jmenoArray; // self.rokDictionary is an NSMutableDictionary
self.jmenoArray=nil;
}
self.element = nil;
}
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 the following XMLParser but when i try to run it, it doesn't work properly.
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:#"results"])
{
currentJob = [SearchResult alloc];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementname isEqualToString:#"jobTitle"])
{
currentJob.jobTitle = currentNodeContent;
}
if ([elementname isEqualToString:#"location"])
{
currentJob.shortAddress = currentNodeContent;
}
if ([elementname isEqualToString:#"companyName"])
{
currentJob.employer = currentNodeContent;
}
if ([elementname isEqualToString:#"results"])
{
[self.jobs addObject:currentJob];
currentJob = nil;
currentNodeContent = nil;
}
}
AND here is my foundCharakter Method:
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
The output doesn't start from the beginning, it starts from the middle of the String...
I just can not understand, why some results look nice where some others don't.
What am i doing wrong ? How can i parse an xml properly ?
Any help will be appreciated.
Thx in advance
I used the following code and works now:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(!elementContentString)
elementContentString = [[NSMutableString alloc] initWithString:string];
else
[elementContentString appendString:string];
}
I don't think there any problem in your code, it is working fine at my end. I am using the twitter api for getting xml and it is giving me proper output.
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"];
}
}