• 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>

  • 相关阅读:
    CSS3权威指南 25.CSS3中的动画功能
    CSS3权威指南 24.CSS3中的变形处理
    CSS3权威指南 23.背景与边框相关样式
    CSS3权威指南 22.盒相关样式
    CSS3权威指南 20.使用选择器在页面中插入内容
    CSS3权威指南 19.选择器
    vivado中如何使用chipscope
    示波器X1探头和X10探头
    两个MMCM共享时钟输入时的严重警告和错误
    ZYNQ基础知识一
  • 原文地址:https://www.cnblogs.com/iphone520/p/2248854.html
Copyright © 2020-2023  润新知