我正在尝试使用SAX与Android解析xml文件,问题是函数字符(...)被多次调用,看起来是相同的数据,只是偏移了几个字符。
从下面的输出中可以看出,第一次用“\ talabama”调用它,第二次用“labama”调用它。我不确定为什么会这样做,但如果有人能提供帮助那就太棒了。
XML输入:
<?xml version="1.0" encoding="utf-8"?>
<dir><name>.</name>
<dir><name>alabama</name>
<dir><name>sub_dir_name</name>
<file><name>file_name.kml</name></file>
</dir>
</dir>
</dir>
字符输出(...):
10-27 23:04:47.033: DEBUG/LocationHandler(10299):
10-27 23:04:49.000: DEBUG/LocationHandler(10299): alabama
10-27 23:04:51.835: DEBUG/LocationHandler(10299): labama
10-27 23:04:52.129: DEBUG/LocationHandler(10299): labama abama
10-27 23:04:52.408: DEBUG/LocationHandler(10299): labama abamasub_dir_name
10-27 23:04:52.519: DEBUG/LocationHandler(10299): ub_dir_name
10-27 23:04:52.649: DEBUG/LocationHandler(10299): ub_dir_name _dir_name
10-27 23:04:52.809: DEBUG/LocationHandler(10299): ub_dir_name _dir_namefile_name.kml
10-27 23:04:52.989: DEBUG/LocationHandler(10299): ile_name.kml
10-27 23:04:53.158: DEBUG/LocationHandler(10299): ile_name.kml le_name.kml
10-27 23:04:53.358: DEBUG/LocationHandler(10299): le_name.kml
10-27 23:04:53.529: DEBUG/LocationHandler(10299): le_name.kml le_name.kml
10-27 23:04:53.698: DEBUG/LocationHandler(10299): le_name.kml
处理程序覆盖:
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
_currentElementValue = "";
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
_currentElementValue += new String(ch);
Log.d(TAG, _currentElementValue);
}
答案 0 :(得分:1)
我在你的代码中看到一个明显的问题,在characters()方法中,你不能只用char数组盲目地创建一个字符串。它应该如下所示:
public void characters(char[] ch, int start, int length) throws SAXException
{
_currentElementValue += new String(ch, start, length);
Log.d(TAG, _currentElementValue);
}
您可能想了解有关SAX解析如何工作的更多信息。