Structure of XML File - Capturing Vertice Data with NSXMLParser - iphone

I have the following xml file which I am parsing using NSXMLParser :
<geometry id="window_strip-mesh" name="window_strip">
<mesh>
<source id="window_strip-mesh-positions">
<float_array id="window_strip-mesh-positions-array" count="1302">399.297 -87842.3 233.334 399.297 -89320.4 233.334 -821.159
...
</float_array>
My question is how can I detect / capture the values that are listed after the > (i.e the 399.297 -87842.3 and so on). Is NSXML Parser able to pick these up ?
Thanks !

you can implement the NSXMLDelegate methods,
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock;
and
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
For your example you have to put the following line in the didStartElement: Method:
if([elementName isEqualToString:#"float_array"]) {
float_array_bool = YES;
}
Then in the - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string; Method you can implement
if (float_array_bool) {
[yourItemToStoreValues.floatArray addObject:string];
}
In the parserDidEndElement: Method you have to unset tho bool value:
if([elementName isEqualToString:#"float_array"]) {
float_array_bool = NO;
}
That should do the trick

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.

consuming a soap l web service in iphone

I am calling the saop web service from the iPhone app. The soap response is a byte []. How can I parse it in the iPhone and display the data in text view? Please help me out.
you have to parse that data friend using nsxmlparserdelegate.
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName { }
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ }
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ }
-(void)parserDidEndDocument:(NSXMLParser *)parser{ }

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;
}
}

Reading id and value for a Node in NSXMLParser

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;
}
}

how to get all values using NSXML parser on iphone

i can only parse 1st element of this xml but i want to get all the content of cuisine
this is my xml
<cuisines>
<cuisine>Asian</cuisine>
<cuisine>Nepalese</cuisine>
<cuisine>North Indian</cuisine>
<cuisine>Indian Vegetarian</cuisine>
<cuisine>Organic Cuisine</cuisine>
</cuisines>
and this is the code to parse
if ( [elementName isEqualToString:#"cuisines"]) {
if ( [elementName isEqualToString:#"cuisine"] ) {
elementName=[[NSMutableString alloc] init];
keyInProgress = [elementName copy];
// This is a string we will append to as the text arrives
textInProgress = [[[NSMutableString alloc] init]autorelease];
return;
}
but its not working i want to get all the value
you need to implement the parser method as well as the following NSXMLParserDelegate protocol methods methods:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
A good tutorial can be found here: Consuming a RESTful Service (bit.ly) in an iPhone Application