本文介绍了如何在iPhone中解析本地存储的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iPhone中解析本地存储的XML文件?

How to parse a locally stored XML file in iPhone?

请使用代码片段帮助我

推荐答案

我使用过NSXMLParser并实现了它。我的资源中有r.xml文件。我刚解析标题并使用NSXMLParser显示。

I have used NSXMLParser and i achieved it. I have r.xml file in my resource. I have just parsing the title and displayed using NSXMLParser.

      r.xml:

      <rss>
      <eletitle > My Xml Program  </eletitle>
      </rss>

这里我的示例代码是,

@interface:

@interface:

  NSXMLParser *rssparser;

  NSMutableArray *stories;
  NSMutableDictionary *item;
  NSMutableString *currrentTitle;
  NSString *currentElement;

@implementation:

@implementation:

    -(void) viewDidAppear:(BOOL) animated
     {
           [self parseXMLFileAtURL];
     }

     -(void) parseXMLFileAtURL
     {
         stories = [[NSMutableArray alloc] init];

         NSURL *xmlURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"r" ofType:@"xml"]];
         rssparser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
        [rssparser setDelegate:self];
        [rssparser setShouldProcessNamespaces:NO];
        [rssparser setShouldReportNamespacePrefixes:NO];
        [rssparser setShouldResolveExternalEntities:NO];
        [rssparser parse];

    }

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

     currentElement = [elementName copy];
     if([elementName isEqualToString:@"rss"]);
     {
         item = [[NSMutableDictionary alloc] init];
         currrentTitle = [[NSMutableString alloc] init];
     }
 }


    -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *) string
     {
            if([currentElement isEqualToString:@"eletitle"])
            {
                 [currrentTitle appendString:string];
            }
      }

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

      if([elementName isEqualToString:@"rss"])
      {
             [item setObject:currrentTitle forKey:@"eletitle"];
             [stories addObject:[item copy]];
      }
   }

     - (void)parserDidEndDocument:(NSXMLParser *)parser
     {
              NSLog(@"The currrentTitle is %@",currrentTitle);

     }

最好的运气。

这篇关于如何在iPhone中解析本地存储的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 16:58