2011-03-23 88 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; 
    }