本文介绍了为什么这不能正确解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,基本上,我正在尝试解析一个子元素:

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

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

我要解析的当前原始文档包含如下示例:sub-element-content1: AB, CD, DE, &显示的东西.

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);
}

这是第一个问题.我遇到的第二个问题是,参考上述内容,应该有 11 个项目.但它只成功抓取了 2 个项目.请注意,除了某些子元素为 null

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

下面是 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.

更新 1:好的,我已经解决了问题.

Update 1:Ok, I have solved the problem.

似乎currentIssue = nil"会导致对象丢失其内容,因为我能够打印出 currentNodeContent.

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

因此,通过删除 currentIssue,我现在可以看到我需要的所有假设元素.

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.

推荐答案

对于 sub element 1 不是 elementName.元素名称为 sub.

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

这篇关于为什么这不能正确解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:49