I have XML like follow how do I parse using touchXML, i want to get data like 26400174350 for <p231:proNum>26400174350</p231:proNum>

StackOverflow https://stackoverflow.com/questions/11137765

Question

  <p231:getTraceDataReturn>
    <p231:proNum>26400174350</p231:proNum>
    <p231:proDate>05/13/2011</p231:proDate>
    <p231:statusCode>DEL</p231:statusCode>
    <p231:status>Delivered</p231:status>
    <p231:appointment/>
    <p231:pieces>7</p231:pieces>
    <p231:weight>1082</p231:weight>
    <p231:po/>
    <p231:bol/>
    <p231:trailer/>
    <p231:signature>BARNES</p231:signature>
    <p231:origTerminal>BIS</p231:origTerminal>
    <p231:origAddress>2300 VERMONT AVE</p231:origAddress>
    <p231:origState>ND</p231:origState>
    <p231:origName>BISMARCK, ND</p231:origName>
    <p231:origCity>BISMARCK</p231:origCity>
    <p231:origZip>58504</p231:origZip>
    <p231:origPhone>(866) 466-5031</p231:origPhone>
    <p231:origFax>(701) 223-3790</p231:origFax>
    <p231:destTerminal>RKI</p231:destTerminal>
    <p231:destAddress>320 31ST AVE</p231:destAddress>
    <p231:destState>IL</p231:destState>
    <p231:destName>ROCK ISLAND, IL</p231:destName>
    <p231:destCity>ROCK ISLAND</p231:destCity>
    <p231:destZip>61201</p231:destZip>
    <p231:destPhone>(800) 728-6806</p231:destPhone>
    <p231:destFax>(309) 794-1496</p231:destFax>
    <p231:delivered>Y</p231:delivered>
    <p231:url/>
    <p231:type>P</p231:type>
    <p231:scac/>
    <p231:errorMessage/>
    <p231:guaranteed>N</p231:guaranteed>
    <p231:call>N</p231:call>
  </p231:getTraceDataReturn>
Was it helpful?

Solution

If you don't mind using NSXMLParser... Here's a complete class code that grabs all <p231:pronum> data and stores on an NSArray for you:

Header:

@interface MyParser : NSObject <NSXMLParserDelegate> {
  NSMutableArray *nodeArray;
  NSMutableString *nodeString;
}

+(NSArray*)parseNodeData:(NSData*)data;
-(NSArray*)nodes;
-(void)parseWithData:(NSData*)data;
@end

Implementation:

@implementation MyParser

+(NSArray*)parseNodeData:(NSData*)data {
  NSArray *ret;
  MyParser *parser = [[MyParser alloc] init];
  [parser parseWithData:data];
  ret = [parser nodes];
  [parser release];
  return(ret);
}

-(NSArray*)nodes {
  if (nodeArray == nil)
    return(nil);
  return([NSArray arrayWithArray:nodeArray]);
}

-(void)dealloc {
  [nodeArray release];

  [super dealloc];
}

-(void)parseWithData:(NSData*)data {
  NSXMLParser parser = [[NSXMLParser alloc] initWithData:data];
  [parser setDelegate:self];
  [parser parse];
  [parser release];
}

#pragma mark - NSXMLParserDelegate methods
-(void)parserDidStartDocument:(NSXMLParser*)parser {
  if (nodeArray != nil)
    [nodeArray release];

  nodeArray = [[NSMutableArray alloc] init];
  [nodeString release];
  nodeString = nil;
}

-(void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName attributes:(NSDictionary*)attributeDict {
  [nodeString release];
  nodeString = [[NSMutableString alloc] init;
}

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string {
  [nodeString appendString:string];
}

-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName {
  if ([elementName compare:@"p231:pronum" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
    [nodeArray addObject:[NSString stringWithString:nodeString]];
  }

  [nodeString release];
  nodeString = nil;
}

@end

That can give your data on an NSArray with only one method call (assuming your have your XML on a NSData object).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top