我需要从dom4j文档类型中删除XML声明
我正在创建文档,使用的方法是
doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);
通过DocumentHelper解析的字符串doc不包含XML声明(它来自XML => XSL => XML转换)。 我认为DocumentHelper会将声明添加到文档主体中?
有没有办法从文档主体中删除XML声明?
doc
我需要从dom4j文档类型中删除XML声明
我正在创建文档,使用的方法是
doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);
通过DocumentHelper解析的字符串doc不包含XML声明(它来自XML => XSL => XML转换)。 我认为DocumentHelper会将声明添加到文档主体中?
有没有办法从文档主体中删除XML声明?
doc
doc.getRootElement().asXML();
我不确定在你的代码中声明具体是什么问题。我曾经在使用dom4j写一个没有声明的xml文件时遇到过类似的问题。
所以,如果这是你的用例:“省略声明”是你要寻找的内容。 http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/OutputFormat.html
Google说这也可以作为属性设置,不确定它的功能是什么。
你需要与根元素进行交互,而不是文档。 例如,使用PhilW提到的默认简洁的OutputFormat:
Document doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);
final Writer writer = new StringWriter();
new XMLWriter(writer).write(doc.getRootElement());
String out = writer.toString();