GDataXMLNode解析xml文件

前端之家收集整理的这篇文章主要介绍了GDataXMLNode解析xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载请标明出处

欢迎交流

zltqzj@163.com

我是一个单线程妞~





NSString* path = [[NSBundle mainBundle] pathForResource:@"StudentXML" ofType:@"xml"];
    NSData* data = [[NSData alloc] initWithContentsOfFile:path];
    GdataxMLDocument* doc = [[GdataxMLDocument alloc] initWithData:data options:0 error:nil];
    GdataxMLElement* root = [doc rootElement];//所有内容
    
    // 取标签中的内容方法:1,路径。2,childAtIndex
    //(1)路径
    NSArray* fistName = [root nodesForXPath:@"//student/name/fistName" error:nil];// 所t有标签为fistName的元素。
    NSLog(@"1:%@",[[fistName objectAtIndex:1] stringValue]);
    // (2)childAtIndex
    GdataxMLNode* lastName = [[[root childAtIndex:1] childAtIndex:0] childAtIndex:1];
    NSLog(@"2:%@",[lastName stringValue]);

代码下载地址:http://download.csdn.net/detail/sijiazhentan/6270435


解析稍微复杂的页面:http://api.hudong.com/iphonexml.do?type=focus-c

代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize recieveData = _recieveData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL* url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
    
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];
    
    
    // Do any additional setup after loading the view from its nib.
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _recieveData = [[NSMutableData alloc] init];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.recieveData  appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString* str = [[NSString alloc] initWithData:self.recieveData encoding:NSUTF8StringEncoding];
    
    _recieveData = nil;
    

    GdataxMLDocument* doc = [[GdataxMLDocument alloc] initWithXMLString:str options:0 error:nil];
    GdataxMLElement* root = [doc rootElement];
    
 
    NSArray* array =[root nodesForXPath:@"//response/docList/docInfo" error:nil];
    for (int i =0; i<array.count; i++) {
         NSLog(@"每项内容的题目:%@",[[[[array objectAtIndex:i] elementsForName:@"docTitle"] objectAtIndex:0] stringValue] );
    }
    
    
   
    
    
    _recieveData = nil;
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
原文链接:https://www.f2er.com/xml/299842.html

猜你在找的XML相关文章