NSXMLParsing for particular tag value

2019-01-27 04:27发布

问题:

I have an XML file that looks like this:

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

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

回答1:

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.



回答2:

This may help

Accessing specific child elements when parsing RSS with NSXMLParser



回答3:

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



回答4:

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;
    }

}



标签: nsxmlparser