1

我正在尝试从网站获取令牌。成功验证后,将显示一个 XML 文档。

我创建了一个连接,如下所示:

NSString *strURLQueryString = [NSString stringWithFormat:@"%@?username=%@&password=%@", kURL_LOGIN, nameString, passwordString];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:strURLQueryString]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:60.0];

为了显示输出,我使用了这个:

unsigned char byteBuffer[[receivedData length]];
[receivedData getBytes:byteBuffer];
NSLog(@"Output: %s", (char *)byteBuffer);

所以返回文档的部分输出如下图所示:

    <status>0</status><reason>User fetched.</reason><token>9cb7396dccabe68c067521db219afb83</token>

我已经阅读了许多 XML 解析实现,但我无法实现它,因为它不能满足我的需要,而且我无法理解其解释的复杂性。

如果有人能给我一个关于如何去做的好建议,我将不胜感激。

4

3 回答 3

1
- (XMLParserViewController *) initXMLParser {


    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict 
{

    if([elementName isEqualToString:@"Books"]) {
        appDelegate.books = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Book"])
    {

        aBook = [[Books alloc] init];

    }

    NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if(!currentElementValue)
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    NSLog(@"Processing Value: %@", currentElementValue);

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"Books"])
        return;

    if([elementName isEqualToString:@"Book"])
    {
       [appDelegate.books addObject:aBook];        
          aBook = nil;
    }
    else if([elementName isEqualToString:@"name"])
    {
        aBook.name=currentElementValue;
    }
    else if([elementName isEqualToString:@"address"])
    {
        aBook.address=currentElementValue;
    }
    else if([elementName isEqualToString:@"country"])
    {
        aBook.country=currentElementValue;
    }        

    currentElementValue = nil;


    NSLog(@"%@",aBook.name);
    NSLog(@"%@",aBook.address);
    NSLog(@"%@",aBook.country);
}

试试这个,,我希望这有效....

于 2013-07-26T13:05:46.033 回答
0

相关的 NSXMLParserDelegate 方法:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName //etc 
{ _inToken = [elementName isEqualToString:@"token"]; }

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName  //etc
{ _inToken = NO; }

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  if (_inToken) {
      _token = string;
      [parser abortParsing];
  }
}
于 2013-07-26T09:28:09.693 回答
0

您应该使用 XML 解析器。它易于使用,请执行以下所有步骤以获得最佳效果:

从 NSXMLParser 创建 XMLParser 类的子类,所以你有一个 NSXMLParser.h 和 NSXMLParser.m 类

你的 .h 类必须是这样的:

#import <Foundation/Foundation.h>

@interface XMLParser : NSXMLParser <NSXMLParserDelegate>{

    NSUInteger parsedCounter;
    BOOL accumulatingParsedCharacterData;
    BOOL didAbortParsing;
}
@property (nonatomic, strong) NSMutableString *currentParsedCharacterData;
@property (nonatomic, strong) NSMutableArray *currentParsedCharacterArray;
@end

和 .m 类:

#import "XMLParser.h"

@implementation XMLParser

#pragma mark Parser constants

// Limit the number of parsed data to 100.
static const NSUInteger kMaximumNumberOfFilesToParse = 100;

static NSUInteger const kSizeOfFileBatch = 10;

// Reduce potential parsing errors by using string constants declared in a single place.
static NSString * const kEntryElementName = @"dlResult";
static NSString * const kStringElementName = @"string";


#pragma mark NSXMLParser delegate methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

//    NSLog(@"didStart");
    // If the number of parsed earthquakes is greater than kMaximumNumberOfEarthquakesToParse, abort the parse.
    if (parsedCounter >= kMaximumNumberOfFilesToParse) {
        // Use the flag didAbortParsing to distinguish between this deliberate stop and other parser errors.
        didAbortParsing = YES;
        [self abortParsing];
    }
    if ([elementName isEqualToString:kEntryElementName]) {
        _currentParsedCharacterArray = [[NSMutableArray alloc]init];

    } else if ([elementName isEqualToString:kStringElementName]) {

        accumulatingParsedCharacterData = YES;
        _currentParsedCharacterData = [[NSMutableString alloc]init];
    }
}

// return string between tags
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//    NSLog(@"foundCh");
    if (accumulatingParsedCharacterData) {
        // If the current element is one whose content we care about, append 'string'
        // to the property that holds the content of the current element.
        [_currentParsedCharacterData appendString:string];
        NSLog(@"currentParsedCharacterData:%@",_currentParsedCharacterData);
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//    NSLog(@"didEnd");

    if ([elementName isEqualToString:kStringElementName]) {
        [_currentParsedCharacterArray addObject:_currentParsedCharacterData];

    }
    accumulatingParsedCharacterData = NO;
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
       if (didAbortParsing == NO) {
        // Pass the error to the main thread for handling.
        [self performSelectorOnMainThread:@selector(handleError:) withObject:parseError waitUntilDone:NO];
    }
}

@end

在 .m 类中有两个 const 字符串:

static NSString * const kEntryElementName = @"dlResult";
static NSString * const kStringElementName = @"string";

这些是字符串标签,你应该实现的标签是“状态”、“原因”、“令牌”

从连接类,向 XMLParser 类发送数据连接,如下所示:

@autoreleasepool {

    // It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
    // because it gives less control over the network, particularly in responding to connection errors.
    //
        XMLParser *parser = [[XMLParser alloc] initWithData:data];

        parser.currentParsedCharacterArray = [NSMutableArray array];
        parser.currentParsedCharacterData = [NSMutableString string];
        [parser setDelegate:parser];

        [parser parse];

        // depending on the total number of earthquakes parsed, the last batch might not have been a "full" batch, and thus
        // not been part of the regular batch transfer. So, we check the count of the array and, if necessary, send it to the main thread.

        if ([parser.currentParsedCharacterArray count] > 0) {
            // send parsed data to another class or ...
            // parser.currentParsedCharacterArray is parsed data
        }
        parser.currentParsedCharacterArray = nil;
        parser.currentParsedCharacterData = nil;
    }

如果你有任何问题,问我!

于 2013-07-26T15:06:39.060 回答