I have continuous tags of img id="#", where # varies from 1 to 9.
The description of tags consists of floating values.
When I use the standard NSXML Parser, i not getting all the values.
My XML for reference:
<img id="1">-0.0111328,-0.0635608,0.152549,0.11211,-0.0250431,
-0.0370875,0.0862391,0.0970791,-0.0195908,
-0.00892297,0.0791795,0.0554013,0.00362028,0.0138572,0.0432729,
0.0253036,-0.0770325,0.14065,0.118424,0.1787,
0.0734354,0.160883,0.101831,0.237038,0.0681151,0.178331,
0.106532,0.224731,0.133766,0.222096,0.165214,0.240752,
-0.0280366,0.106239,0.052094,0.110642,
</img>
How would I parse the above XML?
Kindly, help me out.
Thanx
This is because parser:foundCharacters: does not deliver all characters at once. You need to concatenate all strings that you get between the callbacks of the parser:didStartElement:namespaceURI:qualifiedName:attributes: and parser:didEndElement:namespaceURI:qualifiedName: that you get for the <img> tag.
In the code below, buf is an NSMutableString ivar of your parser delegate.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([qualifiedName isEqualToString:#"img"]) {
buf = [NSMutableString string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([qualifiedName isEqualToString:#"img"]) {
buf = [buf stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"Got %#", buf);
}
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[buf appendString:string];
}
Finally got it... i placed start and end tags for img id = '#'. My structure now looks like this:
<images>
<img id = '1'> -0.0111328,-0.0635608,0.152549,0.11211,-0.0250431,
-0.0370875,0.0862391,0.0970791,-0.0195908,
-0.00892297,0.0791795,0.0554013,0.00362028,0.0138572,0.0432729,
0.0253036,-0.0770325,0.14065,0.118424,0.1787,
0.0734354,0.160883,0.101831,0.237038,0.0681151,0.178331,
0.106532,0.224731,0.133766,0.222096,0.165214,0.240752,
-0.0280366,0.106239,0.052094,0.110642, ....
</img>
<img id = '2'> ...
</img>
....
....
</images>
<mapping>
<map>
<imgid> 1 </imgid>
<keyword> heavy </keyword>
</map>
<map>
<imgid> 2 </imgid>
<keyword> metal </keyword>
</map>
...
...
</mapping>
Placing start and end tags allowed me to parse the whole xml.
Earlier, the start and end tags were for individual images which only resulted in parsing of one img.
This made me add another key point while parsing XML.
Hope this helps others as well.
Related
I'm trying to parse an RSS feed (http://www.themostunrealbeats.com/?feed=rss2) using NSXMLParser. I am having difficulty finding the picture in the article. Here is where the picture is in the RSS feed.
<media:content url="http://themostunrealbeats.files.wordpress.com/2012/03/madeon.png?w=400" medium="image">
<media:title type="html">madeon</media:title>
</media:content>
Specifically, I want http://themostunrealbeats.files.wordpress.com/2012/03/madeon.png. Yet in the delegate method for NSXMLParser, I don't find anything.
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:#"media:content"]) {
NSLog(#"%#", string);
[content appendString:string];
}
}
string has no value. How can I parse this?
// NSXMLParser has a following method
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
// In this method parameter 'attributeDict' will return you all the sub attributes of main attribute.
// In your case its 'url' of Picture.
// I hope this will help you. Check this out.
I am creating an iPhone App in which I have to consume values from a service url.The response from url is of XML format.XML file has same name attributes many times,and those attributes are not static.I mean the no of attributes may increase from time to time. So I couldn't understand how to consume that XML response in iPhone.
My XML response looks like this :
GetUserResponse xmlns="http://schemas.datacontract.org/2004/07"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CompanyList>
<User>
<Address>
<City>Alabaster</City>
<State>Alabama</State>
</Address>
<Employee>
<Name>DrJohn</Name>
</Employee>
</User>
<User>
<Address>
<City>SanFransisco</City>>
<State>California</State>
</Address>
<Employee>
<Name>DrWilliams</Name>
</Employee>
</User>
</CompanyList>
</GetUserResponse>
The thing is I couldn't say there will be specific number of tags as of 2 tags here.They may or may not increase from time to time.I think we should take something like count number of items and extract the values but couldn't understand how?
This is one of many ways to do it. You can simply get the attributeDict and parse all the values from it using key-value.
NSString *element;
- (void) viewDidLoad {
[super viewDidLoad];
cityArr = [[NSMutableArray alloc] init];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([elementName isEqualToString:#“Address”]) {
// initialize stings to store values of child elements
cityStr = [NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:#“City”])
cityStr appendString:string];// Append Values
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#“Address”]) {
[cityArr addObject:cityStr]; // Add strings to array/dictionary and release string
}
}
I'm developing an application that needs to get data from an XML file. Some of the nodes have a lot of characters and I've a problem using this function :
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
For example for the description node of an item I will get only the 20-30 last characters whereas I would get 200 or 300.
I checked this out with a NSLog and it appears the problem comes from here. Do you know what's wrong ?
Thanks for any advice.
SAX parsers do not guarantee to get all characters at once. You may get multiple calls with chunks of characters from any given block; your code should concatenate them into a single string.
The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([qualifiedName isEqualToString:#"myTag"]) {
buf = [NSMutableString string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([qualifiedName isEqualToString:#"myTag"]) {
buf = [buf stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"Got %#", buf);
}
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[buf appendString:string];
}
How can I get some data from the tag of a xml file?
I have a tag like this:
`<link href="http://127.0.0.1:8580/directory/playlists/" rel="alternate" type="application/atom+xml;type=feed" />`
I would like to save the http link: http://127.0.0.1:8580/directory/playlists/
Can I use the NSXMLParser?
Thanks a lot!
You need to implement a delegate for the NSXMLParser that conforms to the NSXMLParserDelegate protocol. In this implementation implement at least this method:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"link"]) {
NSString* href = [attributeDict objectForKey:#"href"];
// Do you stuff with the href
}
}
The value you are after "http://127.0.0.1:8580/directory/playlists/" is called an attribute. It is the attribute of the element "link".
Check out the Apple documentation relating to XML processing and elements and attributes. For example, http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html
I've got a XML file which contains some data I would like to use:
<?xml version="1.0" encoding="UTF-8" ?>
<items>
<item name="product" price="19.95" where="store">
This is the first product.
</item>
<item name="product2" price="39.95" where="online">
This is the second product.
</item>
<item name="product3" price="99.95" where="garagesale">
This is the third product.
</item>
</items>
If I made 4 arrays, one for the name, one for the price, one for where it was bought and one for its description, how would I get the data into the arrays?
I figured using NSXMLParser, but couldn't get name, price, where or the description.
I'm stuck on how to do this.
Any help appreciated.
First you need to create an object that does the parsing. It will instatiate the NSXMLParser instance, set itself as the delegate for the parser and then call the parse message. It can also be responsible for storing your four result arrays:
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:_data];
[parser setDelegate:self];
BOOL result = [parser parse];
The message you are most interested in implementing in your delegate objects is didStartElement. This guy gets called for each element in your XML file. In this callback you can add your name, price & where attributes to their respective arrays.
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
// just do this for item elements
if(![elementName isEqual:#"item"])
return;
// then you just need to grab each of your attributes
NSString * name = [attributeDict objectForKey:#"name"];
// ... get the other attributes
// when we have our various attributes, you can add them to your arrays
[m_NameArray addObject:name];
// ... same for the other arrays
}
To get the value between the tags (e.g. "This is the first product.") you can override - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
in the follwing method
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"item"]) {
NSString *name=[attributeDict objectForKey:#"name"];
NSString *price=[attributeDict objectForKey:#"price"];
NSString *where=[attributeDict objectForKey:#"where"];
}
}
you have to consider the dictionary of item tag as an array and three tag (name,price and where)as the object at index 0,1,2