Reading id and value for a Node in NSXMLParser - iphone

I have an XML node like this:
<State id="1"> AA </State>
How can I read the value 'AA' ? I'm able to read the id value "1" using:
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict { <br>
//Extract the attribute here.
aState.stateId = [[attributeDict objectForKey:#"id"] integerValue];
NSLog(#"Reading id value :%i", aState.stateId);
}
NSLog(#"Processing Element: %#", elementName);
}
But not able to read the value

As far as i remember, to do this you should implement this method
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
Entire code will look like this:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
foundState = [elementName isEqualToString:#"State"];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (foundState) {
[stringBuffer appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if (foundState) {
NSLog(#"AA = %#", stringBuffer);
foundState = NO;
}
}

Related

Is possible parsing xml file that contains only one node by using NSXMLParser

As a beginner, I have read about NSXMLParser and try to implement a parser for parsing an xml
<?xml version="1.0" encoding="UTF-8"?>
<a>Some text here</a>
my implementation
-(OneNodeXMLParser*)initOneNodeXMLParser{
appDelegate = (OneNodeXMLParser*)[[UIApplication sharedApplication]delegate];
return self;
}
-(void)parser:(NSXMLParser*) parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(#"%s",__PRETTY_FUNCTION__,nil);
if([elementName isEqualToString:#"a"]){
// init some varibles
}
NSLog(#"Starting processing");
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(#"%s",__PRETTY_FUNCTION__,nil);
NSLog(#"%s",string);
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(#"%s",__PRETTY_FUNCTION__,nil);
NSLog(#"Finishing processing");
}
What should I modify to parse the file successfully?
In your .h file declare a NSMutableString *store;
And change the following methods like:
-(void)parser:(NSXMLParser*) parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"a"])
{
store = [[NSMutableString alloc] init];
}
NSLog(#"Starting processing");
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(#"%s",string);
if (store != nil)
{
[store appendString:string];
}
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(#"Finishing processing String : %#", store);
}
You can use that string variable store for future use.

how to get the values from the XML

I wants use google weather api in my project. here I am struggling from long time. to get the current temperature. city, humidity,day of week etc
I successfully hit the url. but I not know exact what i do in foundCharacter method to fetch the value.
Thanks
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementname isEqualToString:#"weather"])
{
currentTweet = [Tweet alloc];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementname isEqualToString:#"city"])
{
}
}
Please check the code:
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:#"weather"])
{
currentTweet = [Tweet alloc];
}
else if ([elementname isEqualToString:#"city"])
{
currentTweet.city = [attributeDict valueForKey:#"city"]
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementname isEqualToString:#"weather"])
{
NSLog("Current City: %#",currentTweet.city);
}
}
For this type of xml, you don't want to use the parser:foundCharacters:
Quote from NSXMLPareser reference guide about foundCharacters method:
Sent by a parser object to provide its delegate with a string
representing all or part of the characters of the current element.
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.
For more details: NSXML Parser Reference,
Working of parser,foundCharacters use

How to Using nsxml Parsing in soap response

I want to parse photoids, how can i do this. My soap response is :
2011-08-01 13:14:45.088 SoapSampleApps[1615:207] DONE. Received Bytes: 666
2011-08-01 13:14:45.089 SoapSampleApps[1615:207] <?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:GetTopPicIdsResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><status xsi:type="xsd:boolean">true</status><message xsi:type="xsd:string">Success</message><photoids xsi:type="xsd:string">9,8,20,21,22,23,24,25,26,19</photoids></ns1:GetTopPicIdsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
And my xmlparse delegate code is:
// NSXMLParsr Delegate---------------------
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"SOAP-ENV:Body"]){
}
[xmlValue setString:#""];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"photoids"]) {
[currentItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
}
else if ([elementName isEqualToString:#"SOAP-ENV:Body"]) {
[xmlParseArray addObject:[NSDictionary dictionaryWithDictionary:currentItem]];
}
NSLog(#"xmlArray is : %#",xmlParseArray);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[xmlValue appendString:string];
}
Use simple Dictionary object to parse SOAP Response:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    self.startelement = elementName;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
   [dictionary setObject:string forKey:self.startelement];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}
You can get your values from dictionary like
NSString *photoids = [dictionary objectForKey:#"photoids"];

NSXMLParser Parsing Attributes

How do you extract attributes from XML using NSXML parser??
Heres my xml:
<item>
<title>Button hails 'amazing' win</title>
<description>Jenson Button hailed yesterday's crazy Canadian Grand Prix victory as the best of his Formula One career.
</description>
<link>http://www.skysports.com/story/0,19528,12433_6986809,00.html</link>
<guid isPermaLink="false">12433_6986809</guid>
<pubDate>Mon, 13 Jun 2011 06:21:00 GMT</pubDate>
<category>News Story</category>
<enclosure type="image/jpg" url="http://img.skysports.com/11/06/128x67/Canadian-GP-Jenson-Button-celebrates1_2609288.jpg" length="123456" />
</item>
I need to get the url from the enclosure tags.
Thanks
if([elementName isEqualToString:#"enclosure"])
{
NSString *urlValue=[attributeDict valueForKey:#"url"];
NSString *urlValue=[attributeDict valueForKey:#"type"];
NSString *urlValue=[attributeDict valueForKey:#"length"];
}
you need to use NSXMLParser and its delegate functions
-
(BOOL) parse:(NSData *)xmlData
-(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{
}
then you need to use some thing like this
if([elementName isEqualToString:#"enclosure"])
{
NSMutableDictionary *Dict=[NSMutableDictionary dictionary];
[Dict setObject:[attributeDict valueForKey:#"url"] forKey:#"url"];
[categoryDict setObject:[attributeDict valueForKey:#"type"] forKey:#"type"];
}
The method...
(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
Gives you a dictionary of attributes and their keys (attributeDict)... look for an entry keyed "url" when the elementName is equal to "enclosure"...
Here is explanation:
Example xml with attribute:
Use xml delegate method:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"remoteContent"]){
NSString* href = [attributeDict objectForKey:#"href"];
NSLog(#"href %#",href);
[someArray addObject:href];
}
}

get attribute values from returned XML

This is the xml I am getting from web:
<?xml version="1.0"?>
<abc>87C4A556-B7E5-5AE4-81FE-86BE0C6306E1</abc>
<abc2>P29077758</abc2>
<abc3>55AGD99D</abc3>
<abc4>147</abc4>
<abc5>1286259226</abc5>
<abc6>USA</abc6>
<abc7>US</abc7>
and using this to get attribute:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if( [elementName isEqualToString:#"a"])
{
recordResults = FALSE;
// greeting.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
Something like that, but I don't have any idea how can I get attribute from returned xml and assign those returned variables into my created variable. How can I do this?
http://blancer.com/tutorials/i-phone/76999/parsing-xml-files/ hope this helps !!!
I highly recommend using TouchXML to do your XML parsing.
I personally had all kinds of issues with NSXMLParser and ended up using TouchXML. Works a treat!
You should implement the following methods:-
One way you can use is :-
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
//check that the element is the one you want. If it is then initialize the string in which you want to store the value.
if(elementName isEqualToString:#"abc")
{
tempString = [[NSMutableString alloc] init];
}
}
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[tempString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if(elementName isEqualToString:#"abc")
{
self.StringToStore = tempString;
}
}