我有一个带有 xml 标记的 sax 解析器,其中包含以下文本:“A & amp; B”(那里没有空格 - 添加所以它不会在此处转换为 &)
就好像它被转换了两次并由于与“A”的结果而转义。这是过程:
已下载 XML 文件
InputStream _inputStream = _urlConnection.getInputStream();
BufferedInputStream _bufferedInputStream = new BufferedInputStream(_inputStream);
ByteArrayBuffer _byteArrayBuffer = new ByteArrayBuffer(64);
int current = 0;
while((current = _bufferedInputStream.read()) != -1)
{
_byteArrayBuffer.append((byte)current);
}
FileOutputStream _fileOutputStream = openFileOutput(_file, MODE_PRIVATE);
_fileOutputStream.write(_byteArrayBuffer.toByteArray());
_fileOutputStream.close();
在 endElement 中使用 Sax 转换数据
else if (inLocalName.equalsIgnoreCase(_nodeTitle))
{
_titleValue = currentValue;
currentValue = "";
}
在调试中,当我在处理程序的字符方法中读取它时,与号已经转换并且数据被截断。
我已经看到了很多关于这个的问题,但从来没有一个解决方案。有任何想法吗?
谢谢
解析器:
List<PropertiesList> _theList = null;
try
{
// Create Factory, Parser, Reader, Handler
SAXParserFactory _saxParserFactory = SAXParserFactory.newInstance();
SAXParser _saxParser = _saxParserFactory.newSAXParser();
XMLReader _xmlReader = _saxParser.getXMLReader();
HandlerReps _handler = new HandlerReps(inRegion, inAbbreviation);
_xmlReader.setContentHandler(_handler);
_xmlReader.parse(new InputSource(inStream));
_theList = _handler.getTheList();
}
处理程序:
// Called when Tag Begins
@Override
public void startElement(String uri, String inLocalName, String inQName, Attributes inAttributes) throws SAXException
{
currentElement = false;
}
// Called when Tag Ends
@Override
public void endElement(String inUri, String inLocalName, String inQName) throws SAXException
{
currentElement = false;
// Title
if (inLocalName.equalsIgnoreCase(_nodeValue))
{
if (_stateValue.equalsIgnoreCase(_abbreviation) &&
_countryValue.equalsIgnoreCase(_region))
{
// Construct the object
PropertiesRegion _regionObject = new PropertiesRegion(_titleValue, _address1Value);
cList.add(_regionObject);
Log.d(TAG, _regionObject.toString());
}
_titleValue = "";
_address1Value = "";
}
// Title
else if (inLocalName.equalsIgnoreCase(_nodeTitle))
{
_titleValue = currentValue;
currentValue = "";
}
// Address1
else if (inLocalName.equalsIgnoreCase(_nodeAddress1))
{
_address1Value = currentValue;
currentValue = "";
}
}
// Called to get Tag Characters
@Override
public void characters(char[] inChar, int inStart, int inLength) throws SAXException
{
if (currentElement)
{
currentValue = new String(inChar, inStart, inLength);
currentElement = false;
}
}