【问题标题】:How to remove the unwanted data from parsing result如何从解析结果中删除不需要的数据
【发布时间】:2012-05-10 07:10:51
【问题描述】:

iam 开发一个应用程序。在那个 iam 中使用 xml 解析。那个 xml 文件包含 4 个类别标签和其他标签。从 xml 文件中我想获取类别标签的数据。在获取类别数据时,每次我打印类别值数组。显示正确。但解析完成后,另一个标签数据将附加到最后一个类别名称。我的解析代码如下。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(self.presentvalue)
{
    [presentvalue appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

if(![elementName compare:@"category"])
{
    [categories addObject:presentvalue];
    NSLog(@"%@",presentvalue);
    NSLog(@"Number of elements %@",categories);
}

}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"category"])
{
    presentvalue=[NSMutableString string];  
}
}

而我的xml文件内容是这样的

    <lesson>
   <categories>
   <category id="1">1</category>
   <category id="2">2</category>
   <category id="3">3</category>
   </categories>
   <references>
    <reference id="1" type="youtube" categoryid="2" name="Boyles law">
    <url><![CDATA[http://www.youtube.com/watch?v=J_I8Y-i4Axc]]>
     </url>
    </reference>
    </references>
    </lesson>

从这个 xml 我得到了像 1,2,3 这样的数组值http://www.youtube.com/watch?v=J_I8Y-i4Axc。像这样我得到了结果。所以请告诉我如何获取类别没有额外数据的值。

【问题讨论】:

  • 您能否将这些 NSLog 语句的输出包括在内。您是否只想在数组中获得 1,2 和 3?

标签: iphone ios


【解决方案1】:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"category"]) {
    NSLog(@"Node is found correctly");
    if(presentvalue == nil)
        presentvalue = [NSMutableString string];
    else 
        [presentvalue setString:@""];
}
else {
    presentvalue = nil;
}

}


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

[presentvalue appendString:string];
if (presentvalue) 
{
   [self.tempArray addObject:presentvalue]; // add this line. create a tempArray containing all the strings.
    presentvalue = nil;
}
}

这应该可以帮助您进行解析。希望能帮助到你。快乐编码:)

【讨论】:

  • 没有用。同样的输出将会出现。
  • [self.tempArray addObject:presentvalue]; // 在 presentvalue = nil 上方添加这一行并删除该方法中的 NSLog。创建一个包含所有字符串的 tempArray。打印 tempArray 以检查结果。
【解决方案2】:

检查您的 xml 解析代码。如果开始/结束元素名称是类别。将对象添加到数组中,否则什么也不做。只是晕倒

【讨论】:

  • 第一件事发布你找到的字符代码并找到了 cdata
  • 第二个是您没有将属性分配给类别中的数组。你熟悉属性吗?
  • 第一件事我已经发布了我找到的字符代码。我不需要那个地址,所以我没有写 cdata 代码。第二我不想要那个类别属性值,我只需要类别标签值。
【解决方案3】:

我觉得这条线看起来很奇怪

if(![elementName compare:@"category"])

不应该是这样吗? (没有!)

if([elementName compare:@"category"])

编辑

尝试将其替换为 didEndElement

if([elementName isEqualToString:@"category"])
{
    [categories addObject:presentvalue];
    presentValue = [NSMutableString string];
    NSLog(@"%@",presentvalue);
    NSLog(@"Number of elements %@",categories);
}

【讨论】:

  • 不应该是if ([elementName isEqualToString:@"category"]) 使用compare: 没有意义,当然也不要使用!compare。该语句应该做什么?
  • 没错。反正这个问题不是这样的。
  • 我认为你的 presentValue 仍然指向 didEndElement 并且 foundCharacters 会被调用,即使它不是类别标签。在为最后一个类别分配 presentValue 后,您没有发布或对其进行任何操作,因此它仍然指向最新的类别。尝试在 didEndElement 函数中分配新的 presentValue。你必须删除!就像我之前所说的那样,来自“如果”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
相关资源
最近更新 更多