如何正确解析本地 XML? [英] how to parse the local XML correctly?

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

问题描述

我是 iOS 编程新手;现在我正在尝试解析本地 XML 文件.它工作正常并在 NSLog 中显示名称;但是当我粘贴同样的东西时,它只显示了一次.

I am new in iOS programming; and now I am trying to parse a local XML file. It worked properly and showed the name in NSLog; but when i pasted the same thing over it showed only one time.

谁能指导我如何在日志中再次显示相同的字符串?

Can anyone guide me how will I show the same string again in the log?

这是我第一次尝试解析时的本地 XML,它显示了结果

This is my local XML when first time I tried parsing and it showed the result

    <Author>
    <name>Collin Ruffenach</name>
    <age>23</age>
    <gender>male</gender>
    <Books>
           <Book>
                  <title>Objective C for the iPhone</title>
                  <year>2010</year>
                  <level>intermediate</level>
              </Book>
    </Books>
    </Author>

第二次我尝试了相同的 XML 文件,但两次粘贴了相同的内容:

Second time I tried the same XML file but pasted the same thing couple of times:

    <Author>
<name>Collin Ruffenach</name>
<age>23</age>
<gender>male</gender>
<Books>
       <Book>
              <title>Objective C for the iPhone</title>
              <year>2010</year>
              <level>intermediate</level>
          </Book>
</Books>
   </Author>
   <Author>
<name>Collin Ruffenach</name>
<age>23</age>
<gender>male</gender>
<Books>
       <Book>
              <title>Objective C for the iPhone</title>
              <year>2010</year>
              <level>intermediate</level>
          </Book>
</Books>
    </Author>
    <Author>
<name>Collin Ruffenach</name>
<age>23</age>
<gender>male</gender>
<Books>
       <Book>
              <title>Objective C for the iPhone</title>
              <year>2010</year>
              <level>intermediate</level>
          </Book>
</Books>
    </Author>

但我无法获得所需的结果.

but the required result I was not able to get.

这是我的.m 代码

 -init {
  if(self == [super init]) {
    parser = [[NSXMLParser alloc]
              initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                            pathForResource:@"sample1"      ofType: @"xml"]]];
    [parser setDelegate:self];
    [parser parse];
  }      
 return self;
}

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

       NSLog(@"Started Element : %@", elementName);
              element = [NSMutableString string];

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

       NSLog(@"Element End named: %@ with avalue of: %@", elementName, element);

      }

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

    if(element == nil)
     {

      element = [[NSMutableString alloc] init];
     }

    [element appendString:string];

    }

谁能指导我如何一次又一次地显示我的结果?

Can anyone guide me how will I show my result again and again?

推荐答案

问题不在于您的解析代码,而在于您的 XML 输入.XML 文件必须有一个单个顶级元素,例如:

The problem is not with your parsing code, but with your XML input. An XML file must have a single top-level element, for example:

<Authors>
   <Author>
   ...
   </Author>
   <Author>
   ...
   </Author>
   <Author>
   ...
   </Author>
</Authors>

在您的情况下,XML 解析器在第一个 Author 元素之后停止读取.

In your case, the XML parser stops reading after the first Author element.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆