NSXMLParsing for particular tag value

前端 未结 4 701
南笙
南笙 2020-12-20 07:46

I have an XML file that looks like this:

 
            
                Ireland
              


        
相关标签:
4条回答
  • 2020-12-20 08:31

    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

    0 讨论(0)
  • 2020-12-20 08:39

    This may help

    Accessing specific child elements when parsing RSS with NSXMLParser

    0 讨论(0)
  • 2020-12-20 08:48

    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.

    0 讨论(0)
  • 2020-12-20 08:48

    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 讨论(0)
提交回复
热议问题