Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

ios - Why does this not parse correctly?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...