2011-03-23 86 views
8

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

<?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>[email protected]</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>和所有存在的数据被存储在数组中。所以....

的记录阵列 - >记录阵列 - >每一个记录,数据的阵列....

我一直坐在这里努力工作,这一点使用TBXML这是很容易够抢单节点....但我不能这样做:(

+2

请参阅如果你能接受更多的答案。您似乎对一些未被接受的问题有合理的答案。 – occulus 2011-03-23 15:02:09

回答

14

好吧,您的第一步是创建一个能够解析数据的类。例如,将其称为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文件,并开始解析过程。此外,你正在初始化你的数组并保留它。现在我们来到奶酪。

- (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数组中,你可以操纵所有你想要的。

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

+0

是的!感谢它的工作原理,我也想要它!但我从另一个问题,选择从阵列paticular entrys在桌面单元格中使用....但非常感谢它治愈了我的头痛2天! – MrPink 2011-03-23 17:03:00

+0

@MrPink:你只需要在'cellForRowAtIndexPath'中放置你希望显示在你的表中的哪个'objectAtIndex'。 – 2011-03-23 17:08:26

+0

cell.textLabel.text = [NSString stringWithFormat:@“%@”,[records objectAtIndex:indexPath.row]]; 这是我正在使用的,但是它打印出每个记录中的所有内容 – MrPink 2011-03-23 17:09:13

0

使用苹果的NSXMLParser,这让老派和所有,但它确实有效

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

一旦你的语法分析器命中呼叫parser:didStartElement:namespaceURI:qualifiedName:attributes:委托回拨,而elementName等于Record(从您看来想要的)。

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

希望帮助:)。

小侧面说明:更喜欢使用Apple's“技术”,而不是第三方:这是更有效和可能性是无尽

+2

但是,TBXML的解析速度比NSXMLParser快得多。我使用TBXML,我喜欢它。 :) – 2011-03-23 15:11:36

+0

这是真的,这是我想如何做到这一点。 sudo如何使用tbxml解析我的xml以上? – MrPink 2011-03-23 15:48:10

-2

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

NSXMLParser的所有文档都是here

此外,这里是一个NSXMLParser Tutorial

+7

NSXMLParser比TBXML慢3倍。 “苹果”这个事实并没有让它变得更好。 – Sagiftw 2012-03-27 19:29:39

1

我写了递归函数来解析任何正确创建的XML与TBXML库。

在我的项目中,我有一个类来解析XML。它有一个名为类方法:+(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; 
    }