如何在Android上使用SAXParser实现字符方法 [英] How to implement characters method using SAXParser on Android

查看:54
本文介绍了如何在Android上使用SAXParser实现字符方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SAXParser解析xml,并想知道这是否是实现characters方法的正确方法.

I am parsing xml using the SAXParser and want to know if this is the right way to implement the characters method.

假设有一个名为elementValue的类级别的String变量,并且在startElement方法中将其初始化为".

Assume there's a class-level String variable named elementValue and it is initialized to "" in the startElement method.

这是字符方法:

@Override
public void characters(char[] ch, int start, int length)
{
    String charsToAppend = new String(ch, start, length);
    elementValue = elementValue + charsToAppend;    
}

我们需要追加到到目前为止的值是否正确,并且追加操作正确吗?

Is it correct that we need to append to the value we have so far and is the append being done correctly?

为什么字符方法也给出起始索引?每次不是零吗?

Also, why does the characters method give the start index? Won't it be zero every time?

谢谢.

推荐答案

是的,您需要追加内容,因为不能保证读取的数据块将包含所有元素文本.

Yes, you need to append because there is no guarantee that the chunk of data read will contain all of the element text.

给出了起始位置并需要使用起始位置,因为数组中的其他字符可能是垃圾.

The start position is given and needs to be used because the other characters in the array can be junk.

但是,您应该改用StringBuffer,并调用它的append()方法,如下所示:

However, you should use a StringBuffer instead, and call its' append() method, like so:

public void characters(char[] ch, int start, int length) {
  yourStringBuffer.append(ch, start, length);
}

这篇关于如何在Android上使用SAXParser实现字符方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆