`

[转]NSXMLParser解析多层嵌套xml

阅读更多

 

As promised, here is a little How-I-did-it / How-To.

First off: I am not an experienced SAX-User.. So this approach might be packing the problem at it’s tail, but this is how DOM-Users feel comfortable with ;)

Let’s assume we want to parse the following XML:

tranist.xml

 

  1. <root>  
  2.     <schedules>  
  3.         <schedule id="0">  
  4.             <from>SourceA</from>  
  5.             <to>DestinationA</to>  
  6.             <links>  
  7.                 <link id="0">  
  8.                     <departure>2008-01-01 01:01</departure>  
  9.                     <arrival>2008-01-01 01:02</arrival>  
  10.                     <info>With food</info>  
  11.                     <parts>  
  12.                         <part id="0">  
  13.                             <departure>2008-01-01 01:01</departure>  
  14.                             <arrival>2008-01-01 01:02</arrival>  
  15.                             <vehicle>Walk</vehicle>  
  16.                         </part>  
  17.                         <part id="1">  
  18.                             <departure>2008-01-01 01:01</departure>  
  19.                             <arrival>2008-01-01 01:02</arrival>  
  20.                             <trackfrom>1</trackfrom>  
  21.                             <trackto>2</trackto>  
  22.                             <vehicle>Train</vehicle>  
  23.                         </part>  
  24.                     </parts>  
  25.                 </link>  
  26.                 <link id="1">  
  27.                     ...  
  28.                 </link>  
  29.                 <link id="2">  
  30.                     ...  
  31.                 </link>  
  32.             </links>  
  33.         </schedule>  
  34.         <schedule id="1">  
  35.             ...  
  36.         </schedule>  
  37.         <schedule id="2">  
  38.             ...  
  39.         </schedule>  
  40.     </schedules>  
  41. </root>  

 

n human readable format, this means: We have multiple schedules with from/to etc. These schedules consist of multiple links (different connections for the same route) with departure/arrival etc. These links consist then of multiple parts/sections with various elements which are not sure to be there..

With the let’s find the element called ‘part’ - approach, you won’t get anywhere..

The Basics
So what do we want to achieve? We want a list/array of Schedules, which have the given members. On member is a list/array of Links, also consisting of the given members and a list/array of parts with the respective members.

This is also the basic idea behind my approach: for every new node-container, use a new class/object (an array will also work, but it’s kinda crap..)

Now we have a Schedule class, a Link class and a Part class.

This is an example of the Link class interface:

Link.h

 

  1. #import "Part.h"  
  2. @interface Link : NSObject {  
  3.     NSString *departure;  
  4.     NSString *arrival;  
  5.     NSString *info;  
  6.     NSMutableArray *parts;  
  7. }  
  8. @property (nonatomic, retain) NSString *departure;  
  9. @property (nonatomic, retain) NSString *arrival;  
  10. @property (nonatomic, retain) NSString *info;  
  11. @property (readonly, retain) NSMutableArray *parts;  
  12. - (void)addPart:(Part *)part;  
  13. @end  

 

We use an accessor method for the parts, because it just feels better when dealing with arrays. (Instead of later using [foo.myArray addObject:..] we have [foo addMe:..])

Also we make it easier for us, using retain properties..

The Parser setup
A short introduction into SAX:

The parsing goes node by node and is not nesting-sensitive. That means that first we get root, then schedules, then schedule, then from, then to, then links, then link, then departure etc. As soon as the parser returns you the node for example, you don’t know anymore in what schedule you were. As long as you have a clearly defined structure where always every element must be present, you could do this using a counter, but as soon as you have multiple nodes with no defined count, you have a problem.

What we do is known as recursive parsing. What does this mean? We implement some kind of memory.

In our parser, we have 4 members and 1 method (to make actual use of the parser..):

 

  1. @property (nonatomic, retain) NSMutableString *currentProperty;  
  2. @property (nonatomic, retain) Schedule *currentSchedule;  
  3. @property (nonatomic, retain) Link *currentLink;  
  4. @property (nonatomic, retain) Part *currentPart;  
  5. @property (nonatomic, readonly) NSMutableArray *schedules;  
  6. - (void)parseScheduleData:(NSData *)data parseError:(NSError **)error;  

 

(Yes, this needs to be a NSMutableString..)

Your parseScheduleData method should look similar to the following:

arseJourneyData

 

  1. - (void)parseJourneyData:(NSData *)data parseError:(NSError **)err {  
  2.     NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];  
  3.     self.schedules = [[NSMutableArray alloc] init]; // Create our scheduler list  
  4.     [parser setDelegate:self]; // The parser calls methods in this class  
  5.     [parser setShouldProcessNamespaces:NO]; // We don't care about namespaces  
  6.     [parser setShouldReportNamespacePrefixes:NO]; //  
  7.     [parser setShouldResolveExternalEntities:NO]; // We just want data, no other stuff  
  8.     [parser parse]; // Parse that data..  
  9.     if (err && [parser parserError]) {  
  10.         *err = [parser parserError];  
  11.     }  
  12.     [parser release];  
  13. }  

 

Now we need those delegate methods.

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

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

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

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
This function is called by the parser, when it reads something between nodes. (Text that is..) Like with blah it would read “blah”. It is possible, that this method is called multiple times in one node. As you will see later, we define the property “currentProperty” only if we find a node, we care about. That’s why we test it against this property to make sure, that we need this property. This will then look something like this:

Parser

  1. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string  
  2. {  
  3.     if (self.currentProperty) {  
  4.         [currentProperty appendString:string];  
  5.     }  
  6. }  

 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
This is called, when the parser finds an opening element. In this case, we have a few cases, we need to distinguish. These are:

It’s standard property in the schedule (like <form> etc.) or it’s a deeper nested node (like <links>), the same for all the other nodes.

How to? We define, that we only set a member, if we are in that node. That means, only when we have entered a <part>, then currentPart is set, otherwise it’s nil. The same with the others.

We do then need to check them in reverse order of their nesting level.. Why? Because if we would check for currentLink before currentPart, currentLink would also evaluate to YES/True and hence we will have a problem if their are elements with the same name. If we aren’t in any node, then there is probably a new main node comming -> in the else..

When we hit a nested node, we need to allocate the respective member of our class, so we can use it when the parser gets deeper into it.

This will look like this:

Parser

  1. (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {  
  2.     if (qName) {  
  3.         elementName = qName;  
  4.     }  
  5.     if (self.currentPart) { // Are we in a  
  6.         // Check for standard nodes  
  7.         if ([elementName isEqualToString:@"departure"] || [elementName isEqualToString:@"arrival"] || [elementName isEqualToString:@"vehicle"] || [elementName isEqualToString:@"trackfrom"] || [elementName isEqualToString:@"trackto"] ) {  
  8.             self.currentProperty = [NSMutableString string];  
  9.         }  
  10.     } else if (self.currentLink) { // Are we in a  
  11.         // Check for standard nodes  
  12.         if ([elementName isEqualToString:@"departure"] || [elementName isEqualToString:@"arrival"] || [elementName isEqualToString:@"info"]) {  
  13.             self.currentProperty = [NSMutableString string];  
  14.         // Check for deeper nested node  
  15.         } else if ([elementName isEqualToString:@"part"]) {  
  16.             self.currentPart = [[Part alloc] init]; // Create the element  
  17.         }  
  18.     } else if (self.currentSchedule) { // Are we in a  ?  
  19.         // Check for standard nodes  
  20.         if ([elementName isEqualToString:@"from"] || [elementName isEqualToString:@"to"]) {  
  21.             self.currentProperty = [NSMutableString string];  
  22.         // Check for deeper nested node  
  23.         } else if ([elementName isEqualToString:@"link"]) {  
  24.             self.currentLink = [[Link alloc] init]; // Create the element  
  25.         }  
  26.     } else { // We are outside of everything, so we need a  
  27.         // Check for deeper nested node  
  28.         if ([elementName isEqualToString:@"schedule"]) {  
  29.             self.currentSchedule = [[Schedule alloc] init];  
  30.         }  
  31.     }  
  32. }  

 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
Basically, the same things apply as for didStartElement above. This time, we need to clean things up and assign them if they are set :) This is a bit a pitty, since it’s a lot of code.. *(for not so much)

It’s the same checker-structure..

If we are in a deeper nested node (like <Link>) and we hit an ending element of that nested node (like </Link>), Then we need to add this element to the parent (like <Schedule>) and set it to nil

See yourself:

arser

 

  1. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {  
  2.     if (qName) {  
  3.         elementName = qName;  
  4.     }  
  5.     if (self.currentPart) { // Are we in a  
  6.         // Check for standard nodes  
  7.         if ([elementName isEqualToString:@"departure"]) {  
  8.             self.currentPart.departure = self.currentProperty;  
  9.         } else if ([elementName isEqualToString:@"arrival"]) {  
  10.             self.currentPart.arrival = self.currentProperty;  
  11.         } else if ([elementName isEqualToString:@"vehicle"]) {  
  12.             self.currentPart.vehicle = self.currentProperty;  
  13.         } else if ([elementName isEqualToString:@"trackfrom"]) {  
  14.             self.currentPart.trackfrom = self.currentProperty;  
  15.         } else if ([elementName isEqualToString:@"trackto"]) {  
  16.             self.currentPart.trackto = self.currentProperty;  
  17.         // Are we at the end?  
  18.         } else if ([elementName isEqualToString:@"part"]) {  
  19.             [currentLink addPart:self.currentPart]; // Add to parent  
  20.             self.currentPart = nil; // Set nil  
  21.         }  
  22.     } else if (self.currentLink) { // Are we in a  
  23.         // Check for standard nodes  
  24.         if ([elementName isEqualToString:@"departure"]) {  
  25.             self.currentLink.departure = self.currentProperty;  
  26.         } else if ([elementName isEqualToString:@"arrival"]) {  
  27.             self.currentLink.arrival = self.currentProperty;  
  28.         } else if ([elementName isEqualToString:@"info"]) {  
  29.             self.currentLink.info = self.currentProperty;  
  30.         // Are we at the end?  
  31.         } else if ([elementName isEqualToString:@"link"]) {  
  32.             [currentSchedule addPart:self.currentLink]; // Add to parent  
  33.             self.currentLink = nil; // Set nil  
  34.         }  
  35.     } else if (self.currentSchedule) { // Are we in a  ?  
  36.         // Check for standard nodes  
  37.         if ([elementName isEqualToString:@"from"]) {  
  38.             self.currentSchedule.from = self.currentProperty;  
  39.         } else if ([elementName isEqualToString:@"to"]) {  
  40.             self.currentSchedule.to = self.currentProperty;  
  41.         // Are we at the end?  
  42.         } else if ([elementName isEqualToString:@"schedule"]) { // Corrected thanks to Muhammad Ishaq  
  43.              [schedules addObject:self.currentSchedule]; // Add to the result node  
  44.              self.currentSchedule = nil; // Set nil  
  45.         }  
  46.     }  
  47.     // We reset the currentProperty, for the next textnodes..  
  48.     self.currentProperty = nil;  
  49. }  

 

Finally..
Well, that’s it. You can expand / shrink this principle as you like. You can also add a maxElements counter, like in the SeismicXML example of the iPhone SDK to get only a certain number of elements. You can abort the parser with [parser abortParsing]; It is important, that you don’t abort while in a deeper nested node, because this could lead to inconsistencies. You will need to skip them..

Please note, that I wrote this, while watching TV, so you may need to fix some syntax errors ;) But I hope you get the idea..

from:http://codesofa.com/blog/archive/2008/07/23/make-nsxmlparser-your-friend.html

分享到:
评论

相关推荐

    使用NSXMLParser解析XML数据

    使用NSXMLParser解析XML数据的一个Demo

    ios xml解析 NSXMLParser

    ios xml解析 NSXMLParser。 NSXMLParser是基于SAX的解析方式。NSXMLParser采用了委托设计模式,因此他的实现类需要采用协议并支持委托。NSXMLParser解析XML需要委托NSXMLParserDelegate实现。

    NSXMLParser解析XML

    NULL 博文链接:https://eric-gao.iteye.com/blog/1590902

    iphone NSXMLParser

    代码例子(关于NSXMLParser 解析xml文件) xml 文件 通过NSXMLParser 解析成为数组 元素内容与属性可以获取到

    ios-iOS xml解析.zip

    xml解析的两种常用方式, DOM解析:一次性将整个XML文档加载进内存,比较适合解析小文件,例如:GDataXml解析 ...SAX解析:从根元素开始,按顺序一个元素一个元素往下解析,比较适合解析大文件,例如:NSXMLParser解析

    iphone Reachability NSConnection NSXMLParser

    Reachability 2.0版本,确认网络环境3G/WIFI 使用NSConnection下载数据 使用NSXMLParser解析xml文件

    iOSXML解析器SHXMLParser.zip

    SHXMLParser 是一个基于 NSXMLParser 构建的易用的 XML 解析器,可转换 XML 数据到 Objective-C 对象。 标签:SHXMLParser

    xmlparsing-with-nsxmlparser-tutorial:使用NSXMLParser进行XMLParsing-教程

    xmlparsing-with-nsxmlparser-tutorial 你可以找到的NSXMLParser完整XMLParsing -教程这里 。 本教程由最好的The App Guruz提供。

    iOS开发使用XML解析网络数据

    前言:本篇随笔介绍的是XML解析。 正文: 1、XML解析方式有2两种: DOM:一次性将整个XML数据加载进内存进行解析,比较适合解析小文件SAX:从根元素开始,按顺序... 2-3、本人基于苹果原生NSXMLParser封装好的XML解析

    iOS开发中常见的解析XML的类库以及简要安装方法

    在iPhone开发中,XML的解析有很多选择,iOS SDK提供了NSXMLParser和libxml2两个类库,另外还有很多第三方类库可选,例如TBXML、TouchXML、KissXML、TinyXML和GDataXML。问题是应该选择哪一个呢? 解析 XML 通常有两...

    Soap请求网络数据

    通过SOAP 网络请求数据,并使用NSXMLParser解析Xml

    ios-XMLDictionary.zip

    xml转字典,采用系统NSXMLParser解析,轻轻松松一句代码完成xml转换字典操作

    iPhone编程解析xml

    本程序演示了如何连接浏览器,如何获取数据并解析xml文件。

    ReactiveNSXMLParser:NSXMLParser的ReactiveCocoa扩展

    用于NSXMLParser的ReactiveCocoa扩展:一种简洁的,基于流的API,用于使用NSXMLParser解析XML。 围绕定义包装器,从而不再需要实现繁琐的委托方法。 应用所需的任何ReactiveCocoa魔术(请参阅 ): # import " ...

    使用Swift实现iOS App中解析XML格式数据的教程

    主要介绍了使用Swift实现iOS App中解析XML格式数据的教程,讲到了iOS中提供的NSXMLParser和NSXMLParserDelegate两个API的用法,需要的朋友可以参考下

    iphone中xml的解析demo

    使用iphoneSDK官方NSXMLParserDelegate做的简单xml解析,附带详细注释以及使用到的方法的详细解释,灰常适合新手,高手请留下你们的改进意见。

    在Swift中实现的简单XML解析器-Swift开发

    在Swift中实现的简单XML解析器这是什么? 这是一个受SwiftyJSON和SWXMLHash启发的XML解析器。 Foundation框架中的NSXMLParser是一种“ SAX”解析器。 它具有足够的性能,但是在Swift中实现了一点inc Simple XML ...

    Objective-c对象组装XML

    NSXMLParser *xmlRead = [[NSXMLParser alloc] initWithData:xmlData]; [xmlRead setDelegate:self]; [xmlRead parse]; [xmlRead release]; //get dataTemplate for (int i=0;i&lt;[objects count]; i++) { ...

    SWXMLHash:Swift中的简单XML解析

    SWXML哈希 SWXMLHash是在Swift中解析XML的相对简单的方法。 如果您熟悉NSXMLParser ,则此库是围绕它的简单包装。 从概念上讲,它提供了从XML到数组字典(又名哈希)的转换。 该API从中获得了很多启发。内容要求iOS ...

    SwiftRSSReader:使用Swift 2,Xcode 7,iOS 9构建RSS Reader演示应用程序

    概述基于Swift的RSS阅读器,可将rss feed作为xml从远程服务器加载,并使用NSXMLParser对其进行解析。 解析完成后,它将数据加载到UITableView即显示标题和日期。 当单击标题时,它将转到详细信息页面,在该页面中,...

Global site tag (gtag.js) - Google Analytics