I need to get the value of the following xml , I am using NSXML parser for parsing this
<boolean xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</boolean>
I need to get the "true " value from the above xml .
What should I do inside the NSXML parser delegate methods?
#import "XMLParser.h"
#implementation XMLParser
- (NSString *)parseXMLFile: (NSURL *) url
{
outstring = [[NSMutableString alloc] init];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL: url];
[parser setDelegate: self];
[parser parse];
[parser release];
return [outstring autorelease];
}
//<boolean xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</boolean>
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if (qName) elementName = qName;
if (elementName) current = [NSString stringWithString:elementName];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
current = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!current) return;
if ([current isEqualToString:#"boolean"])
[outstring appendFormat:#"%#\n", string];
}
// <boolean xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</boolean>
-(void) parserDidStartDocument:(NSXMLParser *)parser {
NSLog(#"parserDidStartDocument");
}
-(void) parserDidEndDocument: (NSXMLParser *)parser {
NSLog(#"parserDidEndDocument %#", outstring);
}
#end
For more detail regarding the nsxml parse Reference site here.
this may more help to you.
if you want true value then do following:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
strVal=string;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"boolean"])
{
//you can get 'true' value here
}
}
Related
How do I parse this type of XML using NSXMLParser
<category> <categoryName> userName</categoryName> <categoryName>password</categoryName><category>
Declare array and a string in h file like:
NSMutableArray *aryCategory;
NSString *strCategroyName;
in .m file for starting Parser use:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:yourData]; // your data will be instance of NSData or NSMutableData
parser.delegate = self;
[parser parse];
this will be done once you get your xml data. For handling result you can use NSXMLParserDelegate as follows:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"category"]) {
aryCategory = [[NSMutableArray alloc] init];
strCategroyName = #"";
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
strCategroyName = [NSString stringWithFormat:#"%#%#", strCategroyName, string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"categoryName"]) {
[aryCategory addObject:strCategroyName];
strCategroyName = #"";
}
}
Now you will have your array fill with all of your category name.
Hope this helps :)
Write the <category> in didStart and didEnd elements which are the parser's delegates.
getting pretty despirate here.. how on earth do I parse xml that have attributes in them.. I am completely lost,
this is what the xml that I am trying to parse looks like..
<Rows>
<Row SKATERID="706" MANUFACTURER="A-DZG" ISFACT="F" ISSKATE="F"/>
<Row SKATERID="318" MANUFACTURER="A.R.E." ISFACT="F" ISSKATE="T"/>
//...
</Rows>
Using your NSXMLParser Delegate method:
Updated my code. Here is the code which parses your XML.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:#"Row"])
{
myAttrDict = [[NSDictionary alloc] initWithDictionary:attributeDict];
}
if([elementName isEqualToString:#"Rows"]
{
rowsArray = [[NSMutableArray alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"Row"])
{
[rowsArray addObject:myAttrDict];
[myAttrDict release],myAttrDict = nil;
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(#"Rows Array: %#",rowsArray);
}
I have an XML file that looks like this:
<country>
<routes>
<SourceCountry>Ireland</SourceCountry>
<SourcePort>Larne</SourcePort>
<DestinationCountry>UK</DestinationCountry>
<DestinationPort>Troon </DestinationPort>
</routes>
<routes>
<SourceCountry>Ireland</SourceCountry>
<SourcePort>Larne</SourcePort>
<DestinationCountry>UK</DestinationCountry>
<DestinationPort>Cairnryan </DestinationPort>
</routes>
<routes>
<SourceCountry>Ireland</SourceCountry>
<SourcePort>Belfast</SourcePort>
<DestinationCountry>UK</DestinationCountry>
<DestinationPort>Birkenhead </DestinationPort>
</routes>
<routes>
<SourceCountry>Ireland</SourceCountry>
<SourcePort>Belfast</SourcePort>
<DestinationCountry>Belgium</DestinationCountry>
<DestinationPort>Heysham </DestinationPort>
</routes>
<routes>
<SourceCountry>Belgium</SourceCountry>
<SourcePort>Warrenpoint</SourcePort>
<DestinationCountry>UK</DestinationCountry>
<DestinationPort>Heysham </DestinationPort>
</routes>
I want parse particular data for <SourceCountry> == Ireland and <DestinationCountry> == UK. How can I do this?
Use NSXMLParser
implement the delegate for this class ... <NSXMLParserDelegate>
implement the methods for this parser...
YOU will need the following methods..
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
You can read this tutorial; it is simple to use.
This may help
Accessing specific child elements when parsing RSS with NSXMLParser
You need to use XML parser, Use NSXMLParser to parse you data.
Below is the apple sample code of ImageMap in which they have used the NSXMLParser to parse XML content.
http://developer.apple.com/library/mac/#samplecode/ImageMap/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009015
Try this . . .
int element=0;
[self parseXMLFileAtURL:filepath];
following methods are the delegates of NSXMLParser
- (void)parseXMLFileAtURL:(NSString *)URL
{
NSURL *xmlURL = [NSURL fileURLWithPath:URL];
parser = [[ NSClassFromString(#"NSXMLParser") alloc] initWithContentsOfURL:xmlURL];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(#"found file and started parsing");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if(([elementName compare:#"SourceCountry"])==NSOrderedSame)
element=1;
if(([elementName compare:#"SourcePort"])==NSOrderedSame)
element=2;
if(([elementName compare:#"DestinationCountry"])==NSOrderedSame)
element=3;
if(([elementName compare:#"DestinationPort"])==NSOrderedSame)
element=4;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (element==1)
{
[TotalXMLArray addObject:string];
element=0;
}
if (element==2)
{
[TotalXMLArray addObject:string];
element=0;
}
if (element==3)
{
[TotalXMLArray addObject:string];
element=0;
}
if (element==4)
{
[TotalXMLArray addObject:string];
element=0;
}
}
i need to parse this xml file
<?xml version="1.0" encoding="UTF-8"?>
<imageset>
<category>
<image>SQUIRREL</image>
<image>FOX</image>
<image>TIGER</image>
<image>LION</image>
</category>
</imageset>
i use this code to parse this.
- (void)viewDidLoad {
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Config.xml"]]];
[super viewDidLoad];
[parser setDelegate:self];
[parser parse];
NSLog(#"test.......... %#",test);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"image"]) {
test = [[NSMutableArray alloc]init];
addelements = TRUE;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"image"]) {
addelements = FALSE;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[test addObject:string];
}
But it adds only last object,displays like this in console.
test.......... (
LION,
"\n\n",
"\n"
)
I need to add all image names in the xml file means {SQUIRREL,FOX,TIGER,LION}
how can i done,can any one please help me.
Thank u in advance.
Your problem is this method:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"image"]) {
test = [[NSMutableArray alloc]init];
addelements = TRUE;
}
}
Every time an image tag starts, you overwrite the test variable with a new instance of NSMutableArray (leaking the previous object). You need to change it to something like this:
if (!test) test = [[NSMutableArray alloc]init];
That is: only allocate a new array if there wasn't an old one already.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
[mutableString release];
mutableString = [[NSMutableString alloc] init];
if ([elementName isEqualToString:#"image"]) {
if( !test ) test = [[NSMutableArray alloc] initWithCapacity: 10];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"image"])
[test addObject: mutableString];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[mutableString appendString: string];
}
where test and mutableString are ivars declared in your view controller's header
Note: this is very simplified version.
#Mahesh Babu yes you can also add the image by giving the name only as for example......
[UIImage imageNamed:#"SQUIRREL.jpg"];
I am getting SOAP response and when parsing the response I am getting this: (As you can see from result which is currently stored as NSString is NSLoged itself contain xml format data)
<Menus>
<Table1>
<Restaurant_Name>CHICK-FIL-A</Restaurant_Name>
<Restaurant_id>2748</Restaurant_id>
<Phone>(404) 371-1466</Phone>
<billing_city>DECATUR</billing_city>
<billing_state>GA</billing_state>
<billing_zip_code>30030</billing_zip_code>
<billing_addr1>105 EAST TRINITY PLACE</billing_addr1>
</Table1>
</Menus>
The Code for the above generated response:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:#"GetMenusByZipCodeResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = TRUE;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(recordResults)
{
[soapResults appendString:string];
}
//NSLog(#" soap: %#", soapResults);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
{
if([elementName isEqualToString:#"GetMenusByZipCodeResult"])
{
NSLog(#"soapResult FINAL : %#",soapResults);
recordResults = FALSE;
result.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
The above mention data is result of soapResult.
How to read this data? I tried to reparse it but it is not helping.
Please help me.
If you want a simple XML to NSDictionary converter (including the text nodes), check out http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/
Use an XML parser like NSXMLParser
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html