我们无法正确地转换Unicode字符。我们正在以XML格式提供输入,当我们尝试转换时,我们将无法恢复原始字符串。
这是我正在使用的代码,
StringCarrier OStringCarrier = new StringCarrier();
String SXmlFileData= "<export_candidate_response><criteria><output><lastname>Bhagavath</lastname><firstname>ガネーシュ</firstname></output></export_candidate_response>";
String SResult = "";
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(SXslFileName));
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF8");
OutputStream xmlResult = (OutputStream)new ByteArrayOutputStream();
StreamResult outResult = new StreamResult(xmlResult);
transformer.transform(new StreamSource(
new ByteArrayInputStream(SXmlFileData.getBytes("UTF8"))),outResult);
SResult = outResult.getOutputStream().toString();
}
catch (TransformerConfigurationException OException)
{
//Exception has been thrown
OException.printStackTrace();
return OStringCarrier;
}
catch (TransformerException OException)
{
//Exception has been thrown
OException.printStackTrace();
return OStringCarrier;
}
catch (Exception OException)
{
//Exception has been thrown
OException.printStackTrace();
return OStringCarrier;
}
这是我要获得的输出吗?
告诉您,在此过程中的某个地方,UTF-8中的数据正在由认为正在读取Latin-1的软件读取。它没有告诉您在此过程中发生的位置。因此,您需要分割和征服 - 您需要找到正确的数据正确的最后一点。查看XSLT处理器的字符串。使用1.0处理器有点棘手,但是您可以使用通过确定问题是在转换之前还是之后进行的。如果您使用的是XSLT 2.0处理器,那就很容易:可以使用
<xsl:message select="string-to-codepoints($in)"/>
substring($in, $n, 1)
StringReader
读取而不是将其转换为字节流要安全得多。尝试:
transformer.transform(new StreamSource(
new StringReader(SXmlFileData)),outResult);