iPhone TBXML循环和parsing数据

基本上我有一个返回的XML响应和一个string,我需要通过XML循环,并将所有的信息存储在一个数组中。 这里是xml

<?xml version="1.0" encoding="UTF-8"?> <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.2sms.com/2.0/schema/0310_ResponseReportStandard.xsd" Version="1.0"> <Error> <ErrorCode>00</ErrorCode> <ErrorReason>OK</ErrorReason> </Error> <ResponseData> <Identification> <UserID>jonathan.pink@2sms.com</UserID> </Identification> <Result>2 records were returned</Result> <Detail> <ReportTitle>Message Summary: Today</ReportTitle> <Record> <Destination>447790686158</Destination> <Status>WithNetwork</Status> <GUID><![CDATA[2011-03-22T10:54:22.097Z]]></GUID> <DateSubmitted>2011-03-22T10:54:22.097</DateSubmitted> <DateToSend></DateToSend> <DateSent>2011-03-22T10:54:22.533</DateSent> <DateReceived></DateReceived> <Message><![CDATA[Yet again another test]]></Message> <ID>2011-03-22 10:54:22.250HIHIIOJTFVETW85TS</ID> </Record> <Record> <Destination>447790686158</Destination> <Status>SUCCESS</Status> <GUID><![CDATA[2011-03-22T10:50:40.064Z]]></GUID> <DateSubmitted>2011-03-22T10:50:40.063</DateSubmitted> <DateToSend></DateToSend> <DateSent>2011-03-22T10:50:42.473</DateSent> <DateReceived>2011-03-22T10:50:54.570</DateReceived> <Message><![CDATA[This is a test]]></Message> <ID>2011-03-22 10:50:40.210DRUDVMCEZGETW85TS</ID> </Record> <ReportPage ReportID="775797" ItemsPerPage="25" Page="1" TotalItems="2" /> </Detail> </ResponseData> </Response> 

我需要这2个<records>和所有那里的数据存储在一个数组中。 所以….

一个logging数组 – >logging数组 – >每个logging数据….

我一直在坐在这里试图使用TBXML这是很容易抓住一个单一的节点….但我不能这样做:(

好的,你的第一步就是创build一个能parsing数据的类。 例如,将其RecordParser 。 我们现在需要在头文件中添加一些方法,以及一个NSMutableArray

 @interface RecordParser : NSObject { NSMutableArray *records; } @property(nonatomic,retain)NSMutableArray *records; -(void)loadRecords:(NSString *)records; -(void)traverseElement:(TBXMLElement *)element; @end 

现在,继续,并收取您的实施。 我们现在需要实现这两种方法来做我们希望他们做的事情。

 - (void)loadRecords:(NSString *)records { NSString *someXML = @"http://www.something.com/somexml.xml"; TBXML *tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:someXML]] retain]; records = [NSMutableArray array]; [records retain]; if (tbxml.rootXMLElement) [self traverseElement:tbxml.rootXMLElement]; [tbxml release]; } 

基本上,该方法将抓住有问题的XML文件,并开始parsing过程。 另外,你正在初始化你的数组并保留它。 现在我们来到奶酪。

 - (void) traverseElement:(TBXMLElement *)element { do { if (element->firstChild) [self traverseElement:element->firstChild]; if ([[TBXML elementName:element] isEqualToString:@"Record"]) { TBXMLElement *destination = [TBXML childElementNamed:@"Destination" parentElement:element]; TBXMLElement *status = [TBXML childElementNamed:@"Status" parentElement:element]; TBXMLElement *guid = [TBXML childElementNamed:@"GUID" parentElement:element]; TBXMLElement *dateSub = [TBXML childElementNamed:@"DateSubmitted" parentElement:element]; TBXMLElement *dateToSend = [TBXML childElementNamed:@"DateToSend" parentElement:element]; TBXMLElement *dateSent = [TBXML childElementNamed:@"DateSent" parentElement:element]; TBXMLElement *dateReceived = [TBXML childElementNamed:@"DateReceived" parentElement:element]; TBXMLElement *message = [TBXML childElementNamed:@"Message" parentElement:element]; TBXMLElement *id = [TBXML childElementNamed:@"ID" parentElement:element]; [records addObject:[NSArray arrayWithObjects: [TBXML textForElement:destination], [TBXML textForElement:status], [TBXML textForElement:guid], [TBXML textForElement:dateSub], [TBXML textForElement:dateToSend], [TBXML textForElement:dateSent], [TBXML textForElement:dateReceived], [TBXML textForElement:message], [TBXML textForElement:id],nil]]; } } while ((element = element->nextSibling)); } 

基本上,这个方法所做的就是横切XML文件,寻找具有所需名称的元素,然后从子节点获取数据。 另外,数据被添加到records数组中。 所以基本上,当它完成后,它应该有你想要在你的records数组,你可以操纵你想要的所有数据。

这完全没有经过testing 。 不要责怪我,如果它炸毁你的电脑,并杀死你的猫。 我通常不会把所有的工作都写成这样一个完整的方法,但是我碰巧喜欢TBXML 。 请让我知道,如果它的工作。 我真的很感激知道。

我写了recursion函数来parsing任何正确创build的XML与TBXML库。

在我的项目中,我有一个类来parsingXML。 它有一个名为:+(id)infoOfElement:(TBXMLElement *)元素的类方法

如何使用:

 TBXML *tbxml = [TBXML tbxmlWithXMLData:yourData]; TBXMLElement *rootXMLElement = tbxml.rootXMLElement; id parsedData = [self infoOfElement: rootXMLElement]; //return NSString or NSDictionary ot NSArray of parsed data + (id) infoOfElement: (TBXMLElement*) element { if (element->text) return [TBXML textForElement:element]; NSMutableDictionary *info = [NSMutableDictionary new]; TBXMLAttribute *attribute = element->firstAttribute; if (attribute) { do { [info setValue:[TBXML attributeValue:attribute] forKey:[TBXML attributeName:attribute]]; attribute = attribute -> next; } while (attribute); } TBXMLElement *child = element->firstChild; if (child){ TBXMLElement *siblingOfChild = child->nextSibling; //If we have array of children with equal name -> create array of this elements if ([[TBXML elementName:siblingOfChild] isEqualToString:[TBXML elementName:child]]){ NSMutableArray *children = [NSMutableArray new]; do { [children addObject:[self infoOfElement:child]]; child = child -> nextSibling; } while (child); return [NSDictionary dictionaryWithObject:children forKey:[TBXML elementName:element]]; } else{ do { [info setValue:[self infoOfElement:child] forKey:[TBXML elementName:child]]; child = child -> nextSibling; } while (child); } } return info; } 

使用Apple的NSXMLParser ; 它成了老派和所有,但它真的很有效率。

相应地设置您的XMLParser(使用NSXMLParserDelegate协议)。

一旦你的parsing器命中调用parser:didStartElement:namespaceURI:qualifiedName:attributes:委托callback,并且elementName等于Record (从你似乎想要的)。

Alloc'init'一个NSMutableDictionary 。 然后像上面那样,如果elementName等于Destination那么[myDictionary setObject: elementName forKey: @"Destination"]等等。

希望有帮助:)。

小方面说明:更喜欢使用Apple's “技术”,而不是第三方:它更有效率,可能性是无止境的

最好使用NSXMLParser,因为它是Apple的正式版本。

NSXMLParser的所有文档都在这里 。

另外,这里是一个NSXMLParser 教程 。