遵循bdoughan的回答对我不起作用,因为当文本包含 '& 字符时(例如,在 URL 中或使用 HTML 实体(例如“”)时),我在编组过程中遇到错误。
我能够通过将 customDomHandler
的marshal
方法更改为来解决这个问题
public Source marshal(String et, ValidationEventHandler veh) {
Node node = new SimpleTextNode(et);
return new DOMSource(node);
}
其中SimpleTextNode
实现Node接口如下:
class SimpleTextNode implements Node {
String nodeValue = "";
@Override
public SimpleTextNode(String nodeValue) {
this.nodeValue = nodeValue;
}
@Override
public short getNodeType() {
return TEXT_NODE;
}
// the remaining methods of the Node interface are not needed during marshalling
// you can just use the code template of your IDE...
...
}
PS:我很想将此作为对bdoughan回答的评论,但不幸的是我的名声太小了:-(