【问题标题】:Building DOM with xerces and Java - how to prevent escaping of ampersand使用 xerces 和 Java 构建 DOM - 如何防止 & 符号的转义
【发布时间】:2012-02-11 20:37:57
【问题描述】:

我在 Java 中使用 xerces 来构建 DOM。对于成为 DOM 中文本节点的字段之一,数据是从已经将任何非 ASCII 和/或 XML 特殊字符转换为其实体名称或数字的源传递的,例如“香蕉®”

我知道系统的设计是错误的,数据源不应该这样做,但这是我无法控制的,但我想知道是否有办法以某种方式防止这种情况被转义和变成了“香蕉®”没有先解码? (我知道它会隐式转换它需要的任何字符,因此我可以在解码后输入原始字符)。

示例代码:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();      
    DocumentBuilder db = dbf.newDocumentBuilder();      
    Document dom = db.newDocument();        
    Element root = dom.createElement("Companies");      
    dom.appendChild(root);      
    Element company = dom.createElement("Company");
    Text t = dom.createTextNode("Banana®");        
    company.appendChild(t);     
    root.appendChild(company);      
    DOMImplementationRegistry dir = DOMImplementationRegistry.newInstance(); 
    DOMImplementationLS impl = 
        (DOMImplementationLS)dir.getDOMImplementation("LS");        
    LSSerializer writer = impl.createLSSerializer();
    LSOutput output = impl.createLSOutput();
    output.setByteStream(System.out);
    writer.write(dom, output);

示例输出:

<?xml version="1.0" encoding="UTF-8"?>
<Companies><Company>Banana&amp;#174;</Company></Companies>

【问题讨论】:

    标签: xml-serialization xerces


    【解决方案1】:

    如果你能以某种方式在 CDATA 部分中声明它,它应该按原样传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-21
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多