开发者

NSXMLParsing for particular tag value

开发者 https://www.devze.com 2023-02-16 17:44 出处:网络
I have an XML file that looks like this: <country> <routes> <SourceCountry>Ireland</SourceCountry>

I have an XML file that looks like this:

 <country>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Troon </DestinationPort>
                        </routes>
     开发者_JS百科       <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>

I want parse particular data for <SourceCountry> == Ireland and <DestinationCountry> == UK. How can I do this?


Use NSXMLParser

  1. implement the delegate for this class ... <NSXMLParserDelegate>

  2. implement the methods for this parser...

    YOU will need the following methods..

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

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

You can read this tutorial; it is simple to use.


This may help

Accessing specific child elements when parsing RSS with NSXMLParser


You need to use XML parser, Use NSXMLParser to parse you data. Below is the apple sample code of ImageMap in which they have used the NSXMLParser to parse XML content.

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


Try this . . .

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

following methods are the delegates of 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;
    }

}

0

精彩评论

暂无评论...
验证码 换一张
取 消