• iPhone HTTP获得XML并使用GDataXML解析


    1. [代码][C/C++]代码     
    NSURL *url = [NSURL URLWithString:
      @"http://www.raywenderlich.com/downloads/Party.xml"];

    NSMutableURLRequest* request = [NSMutableURLRequest new];    

    [request setURL:url];    
    [request setHTTPMethod:@"GET"]; 

    NSHTTPURLResponse* response;    
    NSData* data = [NSURLConnection sendSynchronousRequest:request    
    returningResponse:&response 
    error:nil];   

    NSString* responseXMLResult = [[NSString alloc] initWithData:data 

    encoding: CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];    

    NSError *error;

    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString: responseXMLResult

      options:0 error:&error];

    if (doc == nil) { return; }

    NSLog(@"LOG=%@", [[NSString alloc] initWithData:doc.XMLData encoding:NSUTF8StringEncoding]);

    //NSLog(@"%@", doc.rootElement.XMLString);

    NSMutableString *resultString = [[NSMutableString alloc] initWithString:@"XML数据内容: "];

    //NSArray *partyMembers = [doc.rootElement elementsForName:@"Player"];

    NSArray *partyMembers = [doc.rootElement nodesForXPath:@"//Party/Player" error:nil];

    for (GDataXMLElement *partyMember in partyMembers) {

    NSString *_name;
    NSString *_level;
    NSString *_class;

    // Name
    //NSArray *names = [partyMember elementsForName:@"Name"];

    NSArray *names = [partyMember nodesForXPath:@"Name" error:nil];

    if (names.count > 0) {
    GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];
    _name = firstName.XMLString;http://www.huiyi8.com/css3/
    } else continue;

    // Level

    NSArray *levels = [partyMember elementsForName:@"Level"];

    if (levels.count > 0) {
    GDataXMLElement *firstLevel = (GDataXMLElement *) [levels objectAtIndex:0];
    _level = firstLevel.stringValue;
    } else continue;

    // Class
    NSArray *classes = [partyMember elementsForName:@"Class"];
    if (classes.count > 0) {
    GDataXMLElement *firstClass = (GDataXMLElement *) [classes objectAtIndex:0];
    _class = firstClass.stringValue;css3动画

    } else continue;

    [resultString appendFormat:@"Name=%@",_name ];
    [resultString appendFormat:@" Level=%@",_level ];
    [resultString appendFormat:@" Class=%@ ",_class ];

    }

    //textView.text = [[NSString alloc] initWithData:doc.XMLData encoding:NSUTF8StringEncoding];

    textView.text = resultString;

        [url release];

    [doc release];#SinaEditor_Temp_FontName

  • 相关阅读:
    JS实现类似CAD的获取点
    ObjectARX填充
    backbone
    (转)Log4net使用详解
    Log4Net介绍
    (转)Log4net使用详解2
    ASP.NET:利用RegexStringValidator验证字符串格式
    ashx文件的使用
    编译执行和解释执行的区别
    利用 Application_Error 捕获所有异常 .
  • 原文地址:https://www.cnblogs.com/xkzy/p/3813422.html
Copyright © 2020-2023  润新知