0

I'm trying to parse an xml here and am getting my CXMLElement properly. One, for example, logs this

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

but I can't get the string value, in this case Tracked. I've tried

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

but logging this returns null. Anyone know how to get the stringValue of a CXMLElement?

Heres the example 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>

and I parse it using this

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"];
        }
    }
}
Chris Loonam
  • 5,735
  • 6
  • 41
  • 63
  • 1
    please add a sample xml. Maybe for your case it's easier to use the built in xml parser than to use a 3rd party library (I talk from experience ^^); see this answer http://stackoverflow.com/a/7295767/207616 –  Feb 20 '13 at 23:39

1 Answers1

2

How about this:

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

Not able to test it but it's similar to some code I've used before.

Richard Brown
  • 11,346
  • 4
  • 32
  • 43
  • This logs them all, but since there is more than 1 how would I get all their values so I could put each of them in, for example, a label? Maybe put them in an array? – Chris Loonam Feb 21 '13 at 02:54
  • Edited the answer to create an array. You can then use the array to do whatever you want... – Richard Brown Feb 21 '13 at 03:13