Why does this not parse correctly?

2019-09-26 03:09发布

问题:

Ok so basically, I am trying to parse a sub-element:

<element>
   <sub element 1>
   <sub element 2>
   <sub element 3>
<element>

The current original document that I want to parse contain as below for example: sub-element-content1: AB, CD, DE, & SOMETHING DISPLAYED.

I only managed to grab & SOMETHING DISPLAYED. I could not parse the whole sub-element.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSLog(@"CURRENT CONTENT: %@", currentNodeContent);
}

That is the 1st issue. The second issue that I am having is that there are supposed to be 11 items in reference to the above. But it only managed to grab 2 items. Please note that each item contains the sub-elements except that some sub-elements are null

Issues has been added: (null)-(null)-(null)
Issues has been added: sub-element-1 sub-element2 sub-element3

Below is portion of didEndElement:

if ([elementName isEqualToString:@"Issue"]) {
    //add currentIssue object into issues array
    [issues addObject:currentIssue];
    NSLog(@"Issues has been added: %@-%@-%@", currentIssue.sub-element-1, currentIssue.sub-element-2, currentIssue.sub-element-3);
    currentIssue = nil;
    currentNodeContent = nil;
}
if ([elementName isEqualToString:@"sub element 1"]) {
    currentIssue.sub-element-1 = currentNodeContent;
    NSLog(@"sub element 1: %@", currentIssue.sub-element-1);
}
if ([elementName isEqualToString:@"sub element 2"]) {
    currentIssue.sub-element-2 = currentNodeContent;
    NSLog(@"sub element 2: %@", currentIssue.sub-element-2);
}
if ([elementName isEqualToString:@"sub element 3"]) {
    currentIssue.sub-element-3 = currentNodeContent;
    NSLog(@"sub element 3: %@", currentIssue.sub-element-3);
}

Your advice(s) and/or suggestion(s) are greatly appreciated.

Update 1: Ok, I have solved the problem.

It appears that "currentIssue = nil" causes the object to lose its contents as I was able to print out the currentNodeContent.

So, by removing, currentIssue, I am now able to see all the supposed elements that I need.

But there is one more problem. I can see all the sub elements now, but there only one sub element left which was grabbed partially which I do not understand why.

回答1:

For <sub element 1> , sub element 1 is not the elementName . The element name is sub.



回答2:

- (XMLParserViewController *) initXMLParser {


    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

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

    if([elementName isEqualToString:@"Books"]) {
        appDelegate.books = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Book"])
    {

        aBook = [[Books alloc] init];

    }

    NSLog(@"Processing Element: %@", elementName);
}

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

    if(!currentElementValue)
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    NSLog(@"Processing Value: %@", currentElementValue);

}

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

    if([elementName isEqualToString:@"Books"])
        return;

    if([elementName isEqualToString:@"Book"])
    {
       [appDelegate.books addObject:aBook];        
          aBook = nil;
    }
    else if([elementName isEqualToString:@"name"])
    {
        aBook.name=currentElementValue;
    }
    else if([elementName isEqualToString:@"address"])
    {
        aBook.address=currentElementValue;
    }
    else if([elementName isEqualToString:@"country"])
    {
        aBook.country=currentElementValue;
    }        

    currentElementValue = nil;


    NSLog(@"%@",aBook.name);
    NSLog(@"%@",aBook.address);
    NSLog(@"%@",aBook.country);
}

Try this code eith your elements hope this works for you.... :)



回答3:

Ok, I have solved the problem.

It appears that "currentIssue = nil" causes the object to lose its contents as I was able to print out the currentNodeContent.

So, by removing, currentIssue, I am now able to see all the supposed elements that I need.

Secondly, to make it work correctly, I initialized the array under "parserDidStartDocument" instead of "parserDidStartElement".