0

Possible Duplicates:
NSXMLParser on iOS, how do I use it given a xml file
Cocoa/Objective-C : Best Practice to parse XML Document?

I want to parse XML file in Cocoa Application.but no parser for xml work well. Please help for parsing this file or other xml files like this. My Xml File is as Under:

  <book name="Genesis">
    <chapter number="1">
      <verse number="1">At the first God made the heaven and the earth.</verse>
      <verse number="2">And the earth was waste and without form; and it was dark on the face of the deep: and the Spirit of God was moving on the face of the waters.</verse>
      <verse number="3">And God said, Let there be light: and there was light.</verse>
    </chapter>  
  </book>   
 <book name="Genesis">
    <chapter number="1">
      <verse number="1">At the first God made the heaven and the earth.</verse>
      <verse number="2">And the earth was waste and without form; and it was dark on the face of the deep: and the Spirit of God was moving on the face of the waters.</verse>
      <verse number="3">And God said, Let there be light: and there was light.</verse>
    </chapter>  
  </book>   
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Possible duplicates: [1](http://stackoverflow.com/questions/2237757/) [2](http://stackoverflow.com/questions/3729514/) [3](http://stackoverflow.com/questions/2964503/) [4](http://stackoverflow.com/questions/3731453/) [5](http://stackoverflow.com/questions/3731594/) [6](http://stackoverflow.com/questions/5274513/) [&c.](http://stackoverflow.com/search?q=%5Biphone%5D+xml+file+parse) – jscs May 11 '11 at 18:17
  • Here are some samples for **xml** parser. - http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project - http://www.hivestudio.cat/index.php?option=com_content&view=article&id=60:xml-parser-for-iphone&catid=35:technical-note-category&Itemid=76 - http://stackoverflow.com/questions/1021102/parsing-xml-in-iphone-xcode – Chetan Bhalara May 10 '11 at 04:57
  • Where should I put the xml file ??? – Marya Mar 21 '14 at 16:09

2 Answers2

1

There are effectively two approaches to parsing XML - an event driven one (such as that used by NSXMLParser) and a tree approach (such as that used by NSXML).

If you're only after specific elements, then it would probably be a lot easier to use the tree approach used by NSXML as it'll enable you to query XML documents using XPath (and indeed XQuery) to return specific nodes, etc. that you're interested in.

If this sounds like it might be a more fruitful approach that using NSXMLParser to iterate over the whole structure, then I'd recommend reading the Introduction to Tree-Based XML Programming Guide for Cocoa. (The "Querying an XML Document" section should be of particular interest.)

PgmFreek
  • 6,374
  • 3
  • 36
  • 47
0
   - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{     
  if ([elementName isEqualToString:@"book"]) 
   {
     item = [[NSMutableDictionary alloc] init]; 

        item_name = [[NSMutableString alloc] initWithString:[attributeDict valueForKey:@"name"]];
}

// then in the item_name you have the value as Genesis
// try to parse like this
EDIT: after seeing the comment

 if ([elementName isEqualToString:@"verse"]) 
       {

            item_name1 = [[NSMutableString alloc] initWithString:[attributeDict valueForKey:@"number"]];
    }

//In item_name1 you will get the value as 1

Aravindhan
  • 15,608
  • 10
  • 56
  • 71