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

最佳答案

如果您不介意使用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对象上)。

关于iphone - 我有XML,例如跟随我如何使用touchXML进行解析,我想为<p231:proNum> 26400174350 </p231:proNum>获取类似26400174350的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11137765/

10-10 20:30