zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    创建Variant数组
    ASP与存储过程(Stored Procedures)
    FileSystemObject对象成员概要
    Kotlin 朱涛9 委托 代理 懒加载 Delegate
    Kotlin 朱涛 思维4 空安全思维 平台类型 非空断言
    Kotlin 朱涛7 高阶函数 函数类型 Lambda SAM
    Kotlin 朱涛16 协程 生命周期 Job 结构化并发
    Proxy 代理模式 动态代理 cglib MD
    RxJava 设计理念 观察者模式 Observable lambdas MD
    动态图片 Movie androidgifdrawable GifView
  • 原文地址:https://www.cnblogs.com/iphone520/p/2248854.html
Copyright © 2011-2022 走看看