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

iOS - GData解析XML 数据

谷德本
2023-12-01
  1. string数据(数据格式不一致)
NSError *error = nil;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:[responseObject mj_JSONData] error:&error];
        GDataXMLElement *rootElem = [doc rootElement];

 NSLog(@" children %@ ",rootElem.children);
 NSLog(@" childCount %lu ",(unsigned long)rootElem.childCount);
/*
打印的数据
children (
    "GDataXMLElement 0x600001d3bab0: {type:1 name:string xml:\"<string>1111.pdf</string>\"}",
    "GDataXMLElement 0x600001d3bae0: {type:1 name:string xml:\"<string2>2222.pdf</string2>\"}"
)
*/

        NSMutableDictionary *mDic = [NSMutableDictionary new];
        for (GDataXMLElement *pointElement in rootElem.children) {
            NSString *name = [pointElement name];
            NSString *value = [pointElement stringValue];
            NSLog(@" -- name  -- %@ ",name);
            NSLog(@" -- Value -- %@ ",value);
            [mDic setObject:[NSString Jh_replaceNull:value] forKey:[pointElement name]];
        }
        
        NSLog(@" mDic %@ ",mDic);


       /*

      mDic {
         string = "1111.pdf";
         string2 = "2222.pdf";
      }
      */

  1. string数据(数据格式一致)
/*
数据
children (
    "GDataXMLElement 0x600001d3bab0: {type:1 name:string xml:\"<string>1111.pdf</string>\"}",
    "GDataXMLElement 0x600001d3bae0: {type:1 name:string xml:\"<string>2222.pdf</string>\"}"
)
*/

            NSError *error = nil;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:[responseObject mj_JSONData] error:&error];
        GDataXMLElement *rootElem = [doc rootElement];
        
        NSMutableArray *mArr = [NSMutableArray array];
        NSArray *arr = [rootElem elementsForName:@"string"];
        for (GDataXMLElement *Elem in arr) {
            
            NSString *stringValue = Elem.stringValue;
            NSLog(@" stringValue %@ ",stringValue);
            
            NSString *stringValue0 = [stringValue substringToIndex:stringValue.length-4];
            NSLog(@" 截取后的文件 %@ ",stringValue0);
            [mArr addObject:stringValue0];
            
        }



  1. array数据
 /*
数据
(
 "GDataXMLNode 0x60000391c0f0: {type:3 name:text xml:\"[{\"Key\":\"74\",\"Value\":\"测试01\",\"IsKhdj\":true,\"IsXG\":true},{\"Key\":\"75\",\"Value\":\"测试02",\"IsKhdj\":true,\"IsXG\":true}]\"}"
)

直接取 rootElem.stringValue 可取出
NSLog(@" stringValue %@ ",rootElem.stringValue);

  1. 多层数据

根据XML的key取出

 NSString *strPath = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:strPath];
    NSError *error = nil;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:data error:&error];
    GDataXMLElement *rootElem = [doc rootElement];
    NSArray *array = [rootElem elementsForName:@"diqu"];

    for (int i = 0; i < array.count; i++) {
        GDataXMLElement *Elem = array[i];
        NSString *Id = [[Elem attributeForName:@"id"]stringValue];
        NSArray *arr = [Elem children];
        GDataXMLElement *dqbzElem = [arr objectAtIndex:1];
        GDataXMLElement *dqmcElem = [arr objectAtIndex:0];
        
       NSLog(@"Id: %@,%@,%@",Id,[dqbzElem stringValue],[dqmcElem stringValue]);
        
        NSMutableDictionary *mdic = [[NSMutableDictionary alloc]init];
        [mdic setObject:Id forKey:@"id"];
        [mdic setObject:[dqbzElem stringValue]forKey:@"DQBZ"];
        [mdic setObject:[dqmcElem stringValue] forKey:@"DQMC"];
//        NSLog(@" mdic %@ ",mdic);
       [AllArr addObject:mdic];
    }
 类似资料: