2013-02-16 36 views
0

我想在這裏解析一個xml,並正在讓我的CXMLElement正確。其中,例如,記錄此iOS CXMLElement StringValue

<CXMLElement 0x1d8cfdc0 [0x1c556660] TrackDetail <TrackDetail>Tracked</TrackDetail>> 

,但我不能得到的字符串值,在這種情況下Tracked。我試過

CXMLNode *title = [[fd objectAtIndex:indexPath.row] nodeForXPath:@"TrackDetail" error:nil]; 

但是這個記錄返回null。任何人都知道如何獲得CXMLElement的stringValue?

繼承人的示例XML

<TrackResponse> 
<TrackInfo ID="9400110200793563195827"> 
    <TrackSummary> 
    The delivery status for this item has not been updated as of February 07, 2013, 11:34 pm. 
    </TrackSummary> 
    <TrackDetail> 
    Out for Delivery, February 07, 2013, 9:34 am, MASSAPEQUA, NY 11758 
    </TrackDetail> 
    <TrackDetail> 
    Sorting Complete, February 07, 2013, 9:24 am, MASSAPEQUA, NY 11758 
    </TrackDetail> 
    <TrackDetail> 
    Arrival at Post Office, February 07, 2013, 6:00 am, MASSAPEQUA, NY 11758 
    </TrackDetail> 
    <TrackDetail> 
    Depart USPS Sort Facility, February 05, 2013, GRAND FORKS, ND 58201 
    </TrackDetail> 
    <TrackDetail> 
    Processed at USPS Origin Sort Facility, February 04, 2013, 7:13 pm, GRAND FORKS, ND 58201 
    </TrackDetail> 
    <TrackDetail> 
    Accepted at USPS Origin Sort Facility, February 04, 2013, 5:58 pm, EAST GRAND FORKS, MN 56721 
    </TrackDetail> 
    <TrackDetail> 
    Electronic Shipping Info Received, February 04, 2013 
    </TrackDetail> 
    <TrackDetail> 
    Shipment Accepted, February 04, 2013, 2:19 pm, EAST GRAND FORKS, MN 56721 
    </TrackDetail> 
</TrackInfo> 
</TrackResponse> 

,我就用這個

NSArray *arr = [parser nodesForXPath:@"//TrackResponse/TrackInfo/TrackSummary" error:nil]; 
NSArray *p = [parser nodesForXPath:@"//TrackResponse/TrackInfo" error:nil]; 
if ([p count]>0) { 
    for (CXMLElement *resultElement in p) { 
     for(int counter = 0; counter < [resultElement childCount]; counter++) { 
      NSLog(@"%@", [[[resultElement nodesForXPath:@"TrackDetail" error:nil] objectAtIndex:counter] stringValue]); 
      [self.arr1 setObject:[[[resultElement nodesForXPath:@"TrackDetail" error:nil] objectAtIndex:counter] stringValue] forKey:@"det"]; 
     } 
    } 
} 
+0

任何人都知道如何? – 2013-02-16 21:31:53

+1

請添加一個示例xml。也許對於你的情況,使用內置的xml解析器比使用第三方庫更容易(我從經驗談話^^);看到這個答案http://stackoverflow.com/a/7295767/207616 – 2013-02-20 23:39:16

回答

2

這樣如何解析:

NSMutableArray *array = [[NSMutableArray alloc] init]; 
NSArray *p = [parser nodesForXPath:@"//TrackResponse/TrackInfo" error:nil]; 
if ([p count]>0) { 
    for (CXMLElement *resultElement in [[p objectAtIndex:0] elementsForName:@"TrackDetail"]) { 
      [array addObject:[resultElement stringValue]]; 
     } 
    } 
} 

無法測試,但它類似於一些代碼我以前用過。

+0

這將它們全部記錄下來,但由於有超過1個,我將如何獲取所有值,以便我可以將它們中的每一個都放入,例如標籤?也許把它們放在一個數組中? – 2013-02-21 02:54:44

+0

編輯創建數組的答案。然後你可以使用這個數組來做任何你想要的事情...... – 2013-02-21 03:13:51

相關問題