dom4j中XML文档声明的含义

3

我需要从dom4j文档类型中删除XML声明

我正在创建文档,使用的方法是

doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);

通过DocumentHelper解析的字符串doc不包含XML声明(它来自XML => XSL => XML转换)。 我认为DocumentHelper会将声明添加到文档主体中?

有没有办法从文档主体中删除XML声明?

doc
3个回答

7
我选择的更简单的解决方案是:
doc.getRootElement().asXML();

非常简单而且不错。这帮了我! - user1506104

2

2

你需要与根元素进行交互,而不是文档。 例如,使用PhilW提到的默认简洁的OutputFormat:

Document doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);    
final Writer writer = new StringWriter();
new XMLWriter(writer).write(doc.getRootElement());
String out = writer.toString();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接