2011-05-10 109 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