我收到一个带有一些特殊字符的 xml,例如这些 Pre‿charged ‿,当我解析此数据时,我无法解析。在我的 xml 中,我得到 Prex**E2x80?**Charged 或 Pre’charged 有什么方法可以对此进行编码吗? 示例代码:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document document = null;
try
{
builder = factory.newDocumentBuilder();
document = builder.parse(xmlFile); // xml data in string format
document.getDocumentElement().normalize();
}
catch (ParserConfigurationException e)
{
System.out.printf("Failed to parse XML Feed data", e);
}
我尝试过 UTF-8、ANSII 和其他一些编码技术。 编辑 : 读取 xml 数据如下
StringBuffer outputData = new StringBuffer();
URL url;
InputStream is = null;
BufferedReader br;
String line;
try
{
url = new URL(data_url);
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
while ((line = br.readLine()) != null)
{
outputData.append(line.trim());
}
br.close();
}
catch (MalformedURLException mue)
{
LOGGER.error("MalformedURLException while fetching feed data", mue);
}
catch (IOException ioe)
{
LOGGER.error("IOException while fetching feed data", ioe);
}
finally
{
try
{
if (is != null)
{
is.close();
}
}
catch (IOException ioe)
{
// Silent fail
}
}
return outputData.toString();