• XML文件解析


    - (void)loadView {

    UITextView *back = [[UITextView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    back.textColor = [UIColor redColor];
    back.backgroundColor = [UIColor whiteColor];
    self.view = back;

    }



    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
    [super viewDidLoad];

    NSString *headPath = [[NSBundle mainBundle] resourcePath];
    NSString *filePath = [[NSString alloc] initWithFormat:@"%@/enen2.xml",headPath];
    NSString *content = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:content options:1 error:nil];
    GDataXMLElement *rootElement = [document rootElement];

    NSDictionary *dic;
    dic = [self getElementFromXMLDATA:rootElement];
    NSLog(@"%@",dic);
    //
    // [(UITextView *)self.view setText:[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    }


    -(id)getElementFromXMLDATA:(GDataXMLElement *)_theElement
    {
    id result = nil;
    NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc]init] autorelease];
    if ([[_theElement children] count] == 0) {
    if ([[_theElement stringValue] length] != 0) {
    result = [_theElement stringValue];
    }
    else {
    for (GDataXMLNode *keyNode in [_theElement attributes]) {
    [dictionary setObject:[[_theElement attributeForName:[keyNode name]] stringValue] forKey:[keyNode name]];
    }
    result = [NSDictionary dictionaryWithObject:dictionary forKey:[_theElement name]];
    }
    }
    else {
    id _result = nil;
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (GDataXMLElement *childElement in [_theElement children]) {
    _result = [self getElementFromXMLDATA:childElement];
    if ([_result class] != NSClassFromString(@"__NSCFDictionary")) {
    }
    else {
    [array addObject:[self getElementFromXMLDATA:childElement]];
    _result = array;
    }
    }
    [dictionary setObject:_result forKey:[_theElement name]];
    result = dictionary;
    [array release];
    }
    return result;
    }

    +++++++++++++++++++++++++++++++++++++++++++++++++++++

    enen2.xml文件中的内容:

    <?xml version='1.0' encoding='UTF-8'?><brands>

    <brand name="福田汽车">

    <series name="蒙派克"/>

    <series>"MP-X蒙派克"</series>

    <series>"风景"</series>

    <series>"迷迪"</series>

    <series>"传奇"</series>

    <series name= "宝马良驹"/>

    <series age="16" name="sdf"/>

    </brand>

    </brands>

  • 相关阅读:
    Vue.js实现的计算器功能完整示例
    vue实现简易计算器
    Vuex中mutations与actions的区别详解
    两个子组件之间的传值
    JS操作元素节点(非常详细)
    js包装类
    Vue Router 的params和query传参的使用和区别(详尽)
    初步了解生命周期
    简单介绍一下Progressive Web App(PWA)
    webpack学习笔记(阮一峰教程demo)
  • 原文地址:https://www.cnblogs.com/iphone520/p/2248854.html
Copyright © 2020-2023  润新知