[iPhone]XML文件解析 parsing-xml-…
来源:互联网 发布:macd背离源码 编辑:程序博客网 时间:2024/06/01 10:20
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
参考资料:
【参考一】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
For example, say you have a simple XML file such as thefollowing:
<?xmlversion= "1.0" encoding="UTF8">
<articleauthor="John Doe">
<para>Thisis a very shortarticle.</para>
</article>
The parserwould report the following series of events to itsdelegate:
- Startedparsing document
- Found starttag for element
article ---<article> - Foundattribute
author ofelement article,value “John Doe”-----author="John Doe" - Found starttag for element
para -----<para> - Foundcharacters
------Thisis a very short article. 【 element'svalue 】 - Found
endtag para------</para>for element - Found
endtag article----</article>for element - Endedparsing document
使用NSXMLParser步骤方法如下:
方法一:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString*)elementName
namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName
attributes:(NSDictionary*)attributeDict {
if([elementName
//Initializethe array.
appDelegate.books= [[NSMutableArray alloc] init];
}
aBook.bookID= [[attributeDict
}
方法二:主要处理element'svalue
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString*)string {
if(!currentElementValue)
currentElementValue= [[NSMutableString alloc]initWithString:string];
else
[currentElementValueappendString:string];
NSLog(@"ProcessingValue: %@", currentElementValue);
}
方法三:
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString*)elementName
namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qName {
if([elementName
[appDelegate.booksaddObject:aBook];
[aBookrelease];
aBook =nil;
}
}
如参考一中实例,从url获取xml文件,并实例化NSXMLParser对象
NSURL *url =[[NSURL alloc]initWithString:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];
NSXMLParser*xmlParser = [[NSXMLParser alloc]
//Initializethe delegate.实例化解析处理方法的代理(步骤二中定义的代理)
XMLParser*parser = [[XMLParser alloc] initXMLParser];
//Setdelegate
[xmlParser
//Startparsing the XML file.调用代理解析NSXMLParser对象
BOOL success= [xmlParser parse];
if(success)
NSLog(@"NoErrors");
else
NSLog(@"ErrorError Error!!!")
- [iPhone]XML文件解析 parsing-xml-…
- [iPhone]XML文件解析 parsing-xml-files NSXMLParser
- iphone解析XML文件
- XML Parsing Error: XML or text d…
- Parsing XML Files(用NSXMLParser解析xml文件)
- iphone NSXMLParser解析xml文件
- IPHONE parser 解析xml文件
- iphone使用GDataXMLParser解析xml文件小结
- iPhone开发 NSXMLParser解析xml文件
- iphone开发 NSXMLParser解析xml文件
- iphone XML解析
- iphone XML 解析
- Iphone XML报文解析
- iphone 解析xml
- e4x 解析xml
- jquery xml解析
- Android 解析XML
- 4.xml 解析
- gdb-iOS调试
- Three20添加方法
- 新浪微博iOS版SDK“宝玉XP”框…
- 新浪微博iOS版SDK“宝玉XP”框…
- 第十六章: 本地化
- [iPhone]XML文件解析 parsing-xml-…
- 320+xcode4
- 怎么将three20 加入Xcode4 中
- 在Xcode4.2 下编译发布与提交App到…
- 下iOS5 和iOS 4.3版本的函数差异 …
- iPhone开发学习笔记016——用U…
- UIApplication深入研究
- MPMoviePlayerController …
- LA 3523 - Knights of the Round Table