1
  <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>
4

1 回答 1

3

如果您不介意使用NSXMLParser...这是一个完整的类代码,它可以为您获取所有<p231:pronum>数据并存储在其中NSArray

标题:

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

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

执行:

@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

这可以给你的数据NSArray只用一个方法调用(假设你的对象上有你的 XML NSData)。

于 2012-06-21T13:30:09.147 回答