【问题标题】:How to save escaped symbols " after parsing and saving xml file via java dom通过java dom解析和保存xml文件后如何保存转义符号
【发布时间】:2016-05-21 14:18:54
【问题描述】:

例如,我有一个带有下一个内容的 input.xml 文件:

<root>&quot;Hello World!&quot;</root>

然后我阅读并解析:

// parse xml file
DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("input.xml"));
doc.getDocumentElement().normalize();

假设我只是想把它保存在其他地方:

Transformer transformer = TransformerFactory.newInstance().newTransformer();    
Result output = new StreamResult(new FileOutputStream("output.xml"));           
        doc.setXmlStandalone(true);
        Source input = new DOMSource(doc);
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(input, output);       

我得到了 output.xml 文件:

<root>"Hello World!"</root>

我需要做些什么来避免这种替换? 我在任何地方都没有找到任何解决方案。

【问题讨论】:

  • 使用 cdata 存储特定数据
  • 你能告诉我怎么做吗?

标签: java xml escaping


【解决方案1】:

这个问题类似于Is there a Java XML API that can parse a document without resolving character entities?Java XML Parsing: Avoid entity reference resolution

如果你想保留entity reference,你需要一个特殊的XML解析器。

【讨论】:

  • 谢谢。This 稍加修正终于解决了我的问题。
猜你喜欢
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
相关资源
最近更新 更多