当前位置: 首页 > 工具软件 > gdata > 使用案例 >

GData使用

公孙志
2023-12-01


    NSString *path = [[NSBundlemainBundle] pathForResource:@"Party"ofType:@"xml"];

   NSData *data = [NSDatadataWithContentsOfFile:path];

    

//    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

//    NSLog(@"~~~~~~~~~%@", str);

    

   NSError *error;

   GDataXMLDocument *gdataxml = [[GDataXMLDocumentalloc] initWithData:dataoptions:0 error:&error];

   GDataXMLElement *rootElement = [gdataxml rootElement];

   NSArray *arrElement = [rootElement children];

    

    //- (NSArray *)elementsForName:(NSString *)name;注意这个方法是直接通过节点名字取节点

    NSLog(@"====================1%@", rootElement);

    //数组中的元素类型GDataXMLElement

    NSLog(@"====================2%@", arrElement);

    //GDataXMLNode的一个减号方法  - (NSString *)stringValue;

    NSLog(@"====================3%@", [arrElement[0]children]);

    //children数组中只有一个节点了 数组元素的类型为GDataXMLNode

    NSLog(@"====================4%@", [arrElement[1]children]);

    //children数组中不只一个节点 数组元素的类型为GDataXMLElement



    

    //XPath的存在类似于SQL语句的存在

   NSError *err;

    //数组中的元素类型GDataXMLElement

   NSArray *arr = [gdataxml nodesForXPath:@"//Party//Players//Player"error:&err];

//    (

//    "GDataXMLElement 0x7ff512f1a130: {type:1 name:Player xml:\"<Player><Name>Butch</Name><Level>1</Level><Class>Fighter</Class></Player>\"}",

    

//    "GDataXMLElement 0x7ff512f1a160: {type:1 name:Player xml:\"<Player><Name>Shadow</Name><Level>2</Level><Class>Rogue</Class></Player>\"}",

    

//    "GDataXMLElement 0x7ff512f1b380: {type:1 name:Player xml:\"<Player><Name>Crak</Name><Level>3</Level><Class>Wizard</Class></Player>\"}"

//    )

    NSLog(@"--------------------1%@", arr);

    

   NSArray *arr1 = [gdataxml nodesForXPath:@"//Party//Players" error:&err];

//    (

//     "GDataXMLElement 0x7ffcfbc7c020: {type:1 name:Players xml:\"<Players><Player><Name>Butch</Name><Level>1</Level><Class>Fighter</Class></Player><Player><Name>Shadow</Name><Level>2</Level><Class>Rogue</Class></Player><Player><Name>Crak</Name><Level>3</Level><Class>Wizard</Class></Player></Players>\"}"

//     )

    NSLog(@"--------------------2%@", arr1);

 类似资料: