2011-03-22 3 views
2

배열로 설정해야하는 xml 응답이 있습니다. 문제는 내가 각 요소에 액세스하고 테이블 뷰에 사용할 수 있도록 배열에 저장할 필요가있다.복잡한 XML을 배열로 파싱하는 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> 

은 내가 TBXML 객체가 초기화하지만 되풀이 정보, 나는 그것이 루프의 일종이 필요합니다 알고 중 하나를 얻을하지만 난 그 증명 있도록 목표 - 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]; 

그리고 내 fonction이 배열의 변환 :

- (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; 
} 

티 함수는 Records을 나타내는 NSDictionary 개체를 포함하는 배열을 반환합니다. NSDictionary는 사용하기 쉬운 속성을 얻기 위해 사용하기 쉽습니다. [yourDictionary objectForKey:yourXmlNode]. 문서는 여기에 있습니다 : NSDictionary.

+0

이 코드를 구현했지만 배열은 두 번째 수준을 넘어서 아무것도 채우지 않습니다. – MrPink

+0

내 샘플 XML에는 agenciesXmlElement-> firstChild가 두 번째 수준을 얻고 있습니다. 당신은 레벨에서 깊이 들어가기 위해 이것을 다시해야합니다. 당신은 그것을하기 위해 모든 것을 가지고 있습니다, 단지 ** 적응 **합니다. –

+0

@MrPink는이 질문을 해결합니까? –

관련 문제