如何在XML XSLT转换期间处理(R)符号

时间:2014-02-05 17:05:44

标签: java xml xslt utf-8 character-encoding

我有一个UTF-8 XML(作为字符串传递),其中包含以下行:

<LongName>SomeName®</LongName>

在XSLT转换之后,它应该转换为另一个UTF-8 XML。问题仅在于®符号,它被转换为两个符号:®

以下是代码:

public String transform (String inputXML) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    OutputStream os = new ByteArrayOutputStream();
    InputStream transformationFile = getClass().getResourceAsStream(TRANSFORMER_PATH);
    Transformer transformer = factory.newTransformer(new StreamSource(transformationFile));
    InputStream is = new ByteArrayInputStream(inputXML.getBytes(Charset.forName("UTF-8")));
    Source input = new StreamSource(is);

    transformer.transform(input, new StreamResult(os));
    return os.toString();
}

所以问题是 - 如何正确地将®转换为®从UTF-8转换为UTF-8 XML?

2 个答案:

答案 0 :(得分:1)

您的错误是最后一行:

return os.toString();

由于osByteArrayOutputStream,因此必须将字节数组转换为String,并且它将使用当前平台默认编码而不是UTF-8。您可以使用return os.toString("UTF-8");

答案 1 :(得分:0)

而不是

  InputStream is = new ByteArrayInputStream(inputXML.getBytes(Charset.forName("UTF-8")));
  Source input = new StreamSource(is);

 Source input = new StreamSource(StringReader(inputXML));