2011-05-10 108 views
0

可能重複:
NSXMLParser on the iPhone, how do i use it given a xml file (newb here :\)
Cocoa/Objective-C : Best Practice to parse XML Document?如何解析XML文件?

我想解析XML文件中的可可Application.but沒有解析器XML工作well.Please幫助解析該文件或其他XML像這樣的文件。謝謝...我的XML文件是在:

<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> 
+1

可能重複:[1](http://stackoverflow.com/questions/2237757/)[2](HTTP:/ /stackoverflow.com/questions/3729514/)[3](http:// sta [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) – 2011-05-11 18:17:28

+0

下面是** xml **解析器的一些示例。 - 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 – 2011-05-10 04:57:15

+0

我應該在哪裏把XML文件? – Marya 2014-03-21 16:09:13

回答

1

有有效的兩種方法來解析XML - 事件驅動的一個(如由使用的NSXMLParser)和樹的方法(如通過使用NSXML )。

如果您只是在特定元素之後,那麼使用NSXML使用的樹方法可能會容易得多,因爲它可以使您使用XPath(實際上是XQuery)查詢XML文檔以返回特定節點等你感興趣。

如果這聽起來像是使用NSXMLParser遍歷整個結構可能是更有成效的方法,那麼我建議讀the Introduction to Tree-Based XML Programming Guide for Cocoa。 (在「查詢XML文檔」部分應該是特別感興趣的。)

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

//然後在ITEM_NAME你有值創
//嘗試解析這樣
編輯:看了之後註釋

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

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

//在item_name1你將得到的值作爲1次

+0

但我怎樣才能得到詩歌? – user746135 2011-05-10 04:49:35

+0

查看更新的答案... – Aravindhan 2011-05-10 04:53:40

+0

我想要經文文本不是數字,請回復那.. 所有詩歌在一個數組 – user746135 2011-05-10 06:47:24