我需要从 dom4j 文档类型中删除 XML 声明
我正在创建文档
doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);
由 DocumenHelper 解析为 Document doc 的字符串不包含 XML 声明(它来自 XML => XSL => XML 转换)我认为 DocumentHelper 正在向文档正文添加声明?
有没有办法从正文中删除 XML 声明
doc
我需要从 dom4j 文档类型中删除 XML 声明
我正在创建文档
doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);
由 DocumenHelper 解析为 Document doc 的字符串不包含 XML 声明(它来自 XML => XSL => XML 转换)我认为 DocumentHelper 正在向文档正文添加声明?
有没有办法从正文中删除 XML 声明
doc
我选择的更简单的解决方案是
doc.getRootElement().asXML();
我不确定这个声明在你的代码中到底哪里有问题。当我想编写一个没有声明的 xml 文件(使用 dom4j)时,我曾经有过这种情况。
因此,如果这是您的用例:“省略声明”就是您要寻找的。 http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/OutputFormat.html
谷歌表示这也可以设置为属性,但不确定它的作用。
You need to interact with the root element instead of the document. For example, using the default, compact OutputFormat mentioned by PhilW:
Document doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);
final Writer writer = new StringWriter();
new XMLWriter(writer).write(doc.getRootElement());
String out = writer.toString();