iPhone XMLParser帮助 [英] iPhone XMLParser help

查看:123
本文介绍了iPhone XMLParser帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析我的应用程序的XML文件,我不知道如何做到这一点。我浏览了一个XMLParser教程,它工作正常,但教程中的XML文件非常简单,我的XML文件也相当复杂。

I am needing to parse an XML file for my app and I dont have any clue how to do it. I went through one XMLParser tutorial, and it worked fine but the XML file in the tutorial was very simple and my XML file is quite a bit more complex.

这里是xml文件的片段:

here is a snippet of the xml file:

  <?xml version="1.0" encoding="UTF-8"?>
    <digital_tpp cycle="1003" from_edate="0901Z    03/11/10" to_edate="0901Z     04/08/10">
        <state_code ID="AK" state_fullname="Alaska">
            <city_name ID="ADAK ISLAND" volume="AK-1">
                <airport_name ID="ADAK" military="N" apt_ident="ADK" icao_ident="PADK" alnum="1244">
                    <record>
                        <chartseq>10100</chartseq>
                        <chart_code>MIN</chart_code>
                        <chart_name>TAKE-OFF MINIMUMS</chart_name>
                        <useraction></useraction>
                        <pdf_name>AKTO.PDF</pdf_name>
                        <cn_flg>N</cn_flg>
                        <cnsection></cnsection>
                        <cnpage></cnpage>
                        <bvsection>C</bvsection>
                        <bvpage></bvpage>
                        <procuid></procuid>
                        <two_colored>N</two_colored>
                        <civil> </civil>
                        <faanfd15></faanfd15>
                        <faanfd18></faanfd18>
                        <copter></copter>
                    </record>
                    <record>
                        <chartseq>10200</chartseq>
                        <chart_code>MIN</chart_code>
                        <chart_name>ALTERNATE MINIMUMS</chart_name>
                        <useraction></useraction>
                        <pdf_name>AKALT.PDF</pdf_name>
                        <cn_flg>N</cn_flg>
                        <cnsection></cnsection>
                        <cnpage></cnpage>
                        <bvsection>E</bvsection>
                        <bvpage></bvpage>
                        <procuid></procuid>
                        <two_colored>N</two_colored>
                        <civil> </civil>
                        <faanfd15></faanfd15>
                        <faanfd18></faanfd18>
                        <copter></copter>
                    </record>
                </airport_name>
            </city_name>
        </state_code>
    </digital_tpp>

我需要做的是在XML文件中搜索< ; ... icao_ident> 用户指定,然后创建一个字典,其中包含< pdf_name> <每个< record> 的chart_name> 。然后,我将创建一个显示pdf文件的UI。

What I'm needing to do is search the XML file for the <...icao_ident> that the user specifies, then create a dictionary containing the <pdf_name> and <chart_name> for each <record> . I will then create a UI that displays the pdf files.

有人可以指导我学习XML解析器的工作原理吗?或者如果我以错误的方式解决这个问题,我也会接受建议。

Can someone direct me to a good tutorial or explanation of how XML parser works? Or if I'm going about this the wrong way I'd be open to suggestions too.

(XML文件约为8MB)

(the XML file is about 8MB)

推荐答案

我知道这不是最好的方式...

i know this isn't the best way...

我挣扎了2天试图使XML解析器适应我的情况。我无法理解它,可能是因为我只是习惯于在C#中这样做而且obj-c对我来说是新的......

I struggled for 2 days trying to adapt the XML parser to my situation. I couldnt grasp it, probably because I'm just so used to doing this in C# and obj-c is new to me...

所以我所做的就是解析作为一个字符串的整个事情。

So what I did was parsed the whole thing as a string.

我将整个XML文件转换为NSString,然后使用substringToIndex和substringFromIndex来隔离我需要的部分(机场)。然后我使用< / record> 标签创建一个< records> 的数组,然后写了一个for通过获取标记的范围来获取每个数组对象所需的值的循环。

I converted the entire XML file to a NSString, then used substringToIndex and substringFromIndex to isolate the section I needed (the airport). I then used the </record> tag to create an array of <records>, then wrote a for loop that took the values I needed out of the each array object just by getting the range of the tags.

就像我说的那样,这是一个疯狂的解决方案,但我在26行代码中做到了这一切并且效果很好。

Like I said, it was a crazy solution, but I did it all in 26 lines of code and it works great.

NSString *path = [[NSBundle mainBundle] pathForResource:@"dttps" ofType:@"xml"];
NSData *data = [[NSData alloc]initWithContentsOfFile:path];
NSString *xmlString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSRange range = [xmlString rangeOfString:searchedAirport];
xmlString = [xmlString substringFromIndex:range.location];
range = [xmlString rangeOfString:@"/airport_name"];
xmlString = [xmlString substringToIndex:range.location];

NSMutableArray *chartNames = [[NSMutableArray alloc]initWithCapacity:100] ;
NSMutableArray *pdfNames = [[NSMutableArray alloc]initWithCapacity:100] ;
NSArray *charts = [xmlString componentsSeparatedByString:@"</record>"];

NSString *tempString = [[NSString alloc]initWithFormat:@""];

int chartsCount = [charts count]-1;

int x;
for (x=0; x < chartsCount; x=x+1) {

    tempString = [charts objectAtIndex:x];
    range = [tempString rangeOfString:@"<chart_name>"];

    tempString = [tempString substringFromIndex:range.location+12];
    range = [tempString rangeOfString:@"</chart_name>"];
    tempString = [tempString substringToIndex:range.location];

    [chartNames addObject:tempString];

    tempString = [charts objectAtIndex:x];
    range = [tempString rangeOfString:@"<pdf_name>"];
    tempString = [tempString substringFromIndex:range.location+10];
    range = [tempString rangeOfString:@"</pdf_name>"];
    tempString = [tempString substringToIndex:range.location-4];
    [pdfNames addObject:tempString];


}

然后清理......

followed by cleanup...

这篇关于iPhone XMLParser帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆