XML解析

#define urlString @"http://localhost/files/test.xml"

void parseXml1(void)

{

    //  1, GDataXMLDocument

    //  2, GDataXMLElement

    

    NSData *data = [ NSData dataWithContentsOfURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

    NSError* error=nil;

    GDataXMLDocument * doc = [[ GDataXMLDocument alloc]initWithData:data options:0 error:&error];

    if (error) {

        NSLog(@"%@",error);

        return;

    }

 

    NSString* xpath1 = @"/root/books";

    //  注意:   documentnodesForXPath:@"/root/books",返回的是

    //         root这个节点的孩子列表(数组)

    //   =>    documentnodesForXPath:@"/root/books/book1",返回

    //         的是 books的孩子列表

    

    //   =>  如果要取某一个节点的值(是字符串),就要先得到该节点的同胞列表,

    //       然后根据该节点在列表中的位置取出这个节点并调stringValue方法

    //       得到字符串

    

    //  obj代表了books这个节点的内容,是多个节点的数组

    NSArray* obj = [doc nodesForXPath:xpath1 error:&error];

    

//    NSArray* obj = [[doc rootElement] children];

    

    if (error) {

        NSLog(@"%@",error);

        return;

    }

    //可以看到objclass NSArray

    NSLog(@"%@", NSStringFromClass([obj class]));

    //obj这个数组一共有一个元素,就是代表book1

    NSLog(@"数组的元素个数:%lu", [obj count]);

    //obj这个数组的首元素的类型为 GDataXMLElement

    NSLog(@"数组的第一个元素为:%@", NSStringFromClass([obj[0] class]));

    // 定义一个相对xml路径来取出book1下边的数组

    NSString* xpath2 =@"book1";

    

    //用相对xml路径来取数组

    NSArray* infoArray = [obj[0] nodesForXPath:xpath2 error:&error];

    NSLog(@"infoArray的类型为:%@", NSStringFromClass([infoArray class]));

    //infoArray: [ GDataXMLElement类型的元素    ]

    

    NSLog(@"infoArray的元素个数为:%lu", [infoArray count]);

    

    GDataXMLElement* element1 = infoArray[0];

    NSLog(@"element1:%@",[element1 stringValue]);

    

    

    NSArray * array1 = [ obj[0] children];

    NSLog(@"obj[0]的孩子列表数组的元素个数:%lu",[array1 count]);

    

    //array2 4个元素代表book1的四个子节点: name,author, price,summary

    NSArray* array2 = [ infoArray[0] children];

    NSLog(@"[infoArray[0]的孩子列表数组的元素个数:%lu]",[array2 count]);

    

    NSLog(@"书名为:%@", [array2[0] stringValue]);

    NSArray *authorInfoArray = [array2[1] children];

    NSLog(@"作者的名字:%@", [authorInfoArray[0] stringValue]);

    NSLog(@"作者的性别:%@", [authorInfoArray[1] stringValue]);

    NSLog(@"作者的年龄:%@",[authorInfoArray[2] stringValue]);

    NSLog(@"作者的地址:%@",[authorInfoArray[3] stringValue]);

    NSLog(@"书的价格:%@",[array2[2] stringValue]);

    NSLog(@"书的简介:%@", [array2[3] stringValue]);

}

 

void workForXml(void)

{

    NSData* data = [ NSData dataWithContentsOfURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

    NSError* error=nil;

    GDataXMLDocument * document = [[ GDataXMLDocument alloc]initWithData:data options:0 error:&error];

    if (error) {

        NSLog(@"%@",error);

        return;

    }

    //  注意:   documentnodesForXPath:@"/root/books",返回的是

    //         root这个节点的孩子列表(数组)

    //   =>    documentnodesForXPath:@"/root/books/book1",返回

    //         的是 books的的孩子列表

    

    //   =>  如果要取某一个节点的值(是字符串),就要先得到该节点的同胞列表,

    //       然后根据该节点在列表中的位置取出这个节点并调stringValue方法

    //       得到字符串

    

//    NSArray* childArrayOfBooks =[[document nodesForXPath:@"/root/books" error:&error][0] children];

    

    GDataXMLElement *booksNode = [[ document rootElement] children][0];

    

    NSArray* childrenOfBooks = [booksNode children];

    NSMutableArray * bookModelsArray= [[NSMutableArray alloc]init];

    

    for(NSUInteger i=0;i<[childrenOfBooks count];i++)

    {

        GDataXMLElement* book = childrenOfBooks[i];

        NSArray* childrenOfABook = [book children];

        

        //先取作者信息

        NSString* authorName = [[childrenOfABook[1] children][0] stringValue];

        NSString* authorSex = [[childrenOfABook[1] children][1] stringValue];

        NSString* authorAge=[[childrenOfABook[1] children][2] stringValue];

        NSString* authorAddress = [[childrenOfABook[1] children][3] stringValue];

        

        Author* author = [[Author alloc]initWithName:authorName andWithAge:authorAge andWithAddress:authorAddress andSex:authorSex];

        

        //取书名,书价,简介

        NSString* bookName = [childrenOfABook[0] stringValue];

        NSString* bookPrice = [childrenOfABook[2] stringValue];

        NSString* bookSummary = [childrenOfABook[3] stringValue];

        

        Book* bookModel = [[ Book alloc]initWithName:bookName andWithAuthor:author  andWithPrice:bookPrice andWithSummary:bookSummary];

        [bookModelsArray addObject:bookModel];

    }

    

    for(NSUInteger i=0;i<[bookModelsArray count];i++)

    {

        NSLog(@"书本信息:%@",bookModelsArray[i]);

    }

 

}

posted @ 2015-11-22 15:27  向日夏  阅读(244)  评论(0编辑  收藏  举报