NSXMLParsing用于特定标记值

时间:2011-03-15 07:53:09

标签: nsxmlparser

我有一个如下所示的XML文件:

 <country>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Troon </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Cairnryan </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Birkenhead </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>Belgium</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Belgium</SourceCountry>
                <SourcePort>Warrenpoint</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>

我想要解析<SourceCountry> == Ireland and <DestinationCountry> == UK的特定数据。我怎么能这样做?

4 个答案:

答案 0 :(得分:2)

使用 NSXMLParser

  1. 实施此课程的委托...... <NSXMLParserDelegate>

  2. 实现此解析器的方法......

    您将需要以下方法..

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

  3. 你可以阅读this tutorial;它使用起来很简单。

答案 1 :(得分:1)

答案 2 :(得分:1)

您需要使用XML解析器,使用NSXMLParser来解析数据。 下面是ImageMap的Apple示例代码,他们使用NSXMLParser来解析XML内容。

http://developer.apple.com/library/mac/#samplecode/ImageMap/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009015

答案 3 :(得分:1)

试试这个。 。

int element=0;
[self parseXMLFileAtURL:filepath];

以下方法是NSXMLParser的代表

- (void)parseXMLFileAtURL:(NSString *)URL
{   
    NSURL *xmlURL = [NSURL fileURLWithPath:URL];
    parser = [[ NSClassFromString(@"NSXMLParser") alloc] initWithContentsOfURL:xmlURL];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{   
    NSLog(@"found file and started parsing");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{           
    if(([elementName compare:@"SourceCountry"])==NSOrderedSame)
        element=1;
    if(([elementName compare:@"SourcePort"])==NSOrderedSame)
        element=2;
    if(([elementName compare:@"DestinationCountry"])==NSOrderedSame)
        element=3;
    if(([elementName compare:@"DestinationPort"])==NSOrderedSame)
        element=4;


}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{   


}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (element==1)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==2)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==3)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==4)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }

}

相关问题