[转载][iPhone]XML文件解析 parsingxmlfiles NSXMLParser

xml文件解析办法
xml是为了提高web数据交换量而出现的,虽然他现在web应用中并不广泛,但是由于他的格式良好,经常被用做配置文件的格式。比如tomcat的主配置文件server.xml,web.xml等等。
 
首先我们看一下需求。我们的目的主要是提取xml文件中的特定内容,又因为xml文件本身的格式良好,这种查询是非常有规律的,非常有利于我们找到需要的信息。有时我们还可能把特定信息写回xml中,但是这种需求并不是必需的,因为配置文件都不会太大,我们完全可以通过手工办法进行修改。
 
对xml进行解析的标准有两种,sax以及dom。
首先这两种标准并不是针对java的,他们在各种语言环境下都可以实现。dom是真正的国际标准。sax是事实的标准,他不由任何商业组织维护,而是由一个非商业的组织在运作。就像iso7层模型和tcp/ip一样,虽然sax不是正式的标准,但是一点不影响他在xml解析领域的地位。
 
dom实现的原理是把整个xml文档一次性读出,放在一个树型结构里。在需要的时候,查找特定节点,然后对节点进行读或写。他的主要优势是实现简单,读写平衡;缺点是比较占内存,因为他要把整个xml文档都读入内存,文件越大,这种缺点就越明显。
 
sax的实现方法和dom不同。他只在xml文档中查找特定条件的内容,并且只提取需要的内容。这样做占用内存小,灵活,正好满足我们的需求。他的缺点就是写,有些资料介绍了写入的方法,但是我感觉这对本例没有必要。
 
 

NSXMLParser 实现的是sax方法解析xml文件。
参考资料:
【参考一】http://www.iphonesdkarticles.com/2008/11/parsing-xml-files.html

【参考二】http://codesofa.com/blog/archive/2008/07/23/make-nsxmlparser-your-friend.html

Introduction

NSXMLParser is a forward only reader or an event driven parser. What it means is, an event is raised whenever the parser comes across a start of an element, value, CDATA and so on. The delegate of NSXMLParser can then implement these events to capture XML data. Some of the events are raised multiple times like the start of an element, value of an element and so on. Since NSXMLParser is known as an event driven parser, we can only read data at the present node and cannot go back. The iPhone only supports NSXMLParser and not NSXMLDocument, which loads the whole XML tree in memory.


For example, say you have a simple XML file such as the following:

 <?xml version= "1.0" encoding="UTF8">
 <article author="John Doe">
 <para>This is a very short article.</para>
 </article>

The parser would report the following series of events to its delegate:

  1. Started parsing document

  2. Found start tag for element article ---<article >

  3. Found attribute author of element article, value “John Doe”-----author="John Doe"

  4. Found start tag for element para -----<para>

  5. Found characters ------This is a very short article. 【 element's value】

  6. Found end tag for element para------</para>

  7. Found end tag for element article----</article>

  8. Ended parsing document

 

使用NSXMLParser步骤方法如下:

 

       

       步骤一,确定从xml文件解析出来的数据如何保存--定义一个类(结构体)

                     小提示:可能一个xml文件解析出的数据包含多个对象,可以用NSMutableArray来保存。如参考一中提到的books

   步骤二自定义解析处理方法(主要三个方法)--Delegate代理

       为了使代码清晰 可以单独提出来作为一个类

方法一:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {

   

if([elementName isEqualToString:@"Books"]) {
//Initialize the array.
appDelegate.books = [[NSMutableArray alloc] init];
}

aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];

}

方法二:主要处理element's value 主要一般都是如下处理保存value值到字符串

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];

NSLog(@"Processing Value: %@", currentElementValue);

}

方法三:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

   

if([elementName isEqualToString:@"Book"]) {
[appDelegate.books addObject:aBook];

[aBook release];
aBook = nil;
}

}

    步骤三 获取xml文件,并建立NSXMLParser对象,后解析

如参考一中实例,从url获取xml文件,并实例化NSXMLParser对象

NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

//Initialize the delegate.实例化解析处理方法的代理(步骤二中定义的代理)
XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate 设置
NSXMLParser对象的解析方法代理
[xmlParser setDelegate:parser];

//Start parsing the XML file.调用代理解析
NSXMLParser对象
BOOL success = [xmlParser parse];

if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");

原文地址:https://www.cnblogs.com/zhwl/p/2871731.html