[英]In Java I need to convert an XML Document to a string, with non-printable characters in data as hex
I have a method that takes a Document and produces an XML String value. 我有一个采用Document并产生XML String值的方法。 It works fine, except that spaces, tabs, and other characters like that are preserved as-is in the node values.
除了在节点值中按原样保留空格,制表符和其他类似字符外,它工作正常。 I need them converted to their hex equivalents.
我需要将它们转换为等效的十六进制。
Here's the method I have: 这是我的方法:
public static String docToXML( Document doc )
{
try
{
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
}
catch (Exception ex)
{
throw new RuntimeException("Error converting to String", ex);
}
}
Even if the value is entered into the document in hex form, it is converted to a space or tab as it's converted to a String. 即使该值以十六进制形式输入到文档中,也将在转换为字符串时转换为空格或制表符。
Does anyone know how to make this happen? 有谁知道如何做到这一点? I'm assuming it's an Output Property, but I haven't found one.
我假设它是一个输出属性,但是我还没有找到。
EDIT: 编辑:
The current behavior is something like this (for a space): 当前行为是这样的(对于空格):
<MyField> </MyField>
The desired behavior is: 所需的行为是:
<MyField> </MyField>
With XSLT 2.0 you can use character maps to achieve this: 使用XSLT 2.0,您可以使用字符映射来实现此目的:
<xsl:character-map>
<xsl:output-character character=" " string="&#x20;"/>
<xsl:output-character character="	" string="&#x09;"/>
...
</xsl:character-map>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.