سؤال

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?

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>

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.

This is my .m code

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

هل كانت مفيدة؟

المحلول

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>

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top