2011-03-22 120 views
2

我有一個XML響應,我需要設置到一個數組。問題是我需要訪問每個元素並將其存儲在一個數組中,以便我可以將它用於tableview。TBXML解析複雜的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>[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> 

我有TBXML對象初始化但不能工作如何得到任何的重複信息,我知道這將需要某種形式的循環,但我是很新,Objective-C的所以它的證明一個挑戰。我需要從xml中獲取<destination> , <status>, <datesent>, <message>。可以有多達25條記錄....

希望有人能幫助這一直在我的腦海中整天!

回答

6

我做了類似的事情最近,你能很快適應,我的XML是:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<agencies> 
    <agency> 
     <name>Agency 1 name</name> 
     <addressFirstLine>Immeuble XYZ<addressFirstLine> 
     <addressSecondLine>rue de la republique</addressSecondLine> 
     <addressThirdLine>69007 Lyon</addressThirdLine> 
     <telNumber>01 23 45 67 89</telNumber> 
    </agency> 
    <agency> 
     <name>Agency 2 name</name> 
     <addressFirstLine>Immeuble ABC<addressFirstLine> 
     <addressSecondLine>rue de la republique</addressSecondLine> 
     <addressThirdLine>69007 Lyon</addressThirdLine> 
     <telNumber>01 23 45 67 89</telNumber> 
    </agency> 
</agencies> 

的代碼我用來分析這一(獲得的結果在我的NSArray稱爲agencies):

TBXML *tbxml = [[TBXML tbxmlWithXMLFile:yourXmlFile retain]; 
    TBXMLElement *rootXMLElement = tbxml.rootXMLElement; 

    if (rootXMLElement) { 
     self.agencies = [self traverseElement:rootXMLElement]; 
     [delegate managerDidReceiveData]; 
    } 

    // release resources 
    [tbxml release]; 

我的功能轉換爲:

- (NSArray *)traverseElement:(TBXMLElement *)element { 

    NSMutableArray *tmpAgencies = [[NSMutableArray alloc] init]; 

    TBXMLElement *agenciesXmlElement = element->firstChild; 
    TBXMLElement *agencyXmlElement; 

    do { 

     // if the element has child elements, process them 
     if ((agencyXmlElement = agenciesXmlElement->firstChild)) { 

      NSMutableDictionary *tmpAgency = [[NSMutableDictionary alloc] init]; 

      do { 
       [tmpAgency setValue:[TBXML textForElement:agencyXmlElement] forKey:[TBXML elementName:agencyXmlElement]]; 

      // Obtain next sibling element 
      } while ((agencyXmlElement = agencyXmlElement->nextSibling)); 

      [tmpAgencies addObject:tmpAgency]; 
      [tmpAgency release]; 
     } 

    // Obtain next sibling element 
    } while ((agenciesXmlElement = agenciesXmlElement->nextSibling)); 

    return tmpAgencies; 
} 

Thi s函數會返回一個數組,其中包含代表您的RecordsNSDictionary對象。 NSDictionary使用簡單,獲得您使用的財產[yourDictionary objectForKey:yourXmlNode]。該文檔位於:NSDictionary

+0

我已經實現了這個,但是我得到的數組只有第二級以外的任何東西...... – MrPink 2011-03-23 13:44:06

+0

我的示例XML中只有兩個級別,agenciesXmlElement-> firstChild正在獲得第二級。你必須再次這樣做才能深入到關卡中。你有一切在這裏做,只是**適應**。 – 2011-03-23 14:27:04

+0

@MrPink這個問題解決了嗎? – 2011-03-28 09:10:09