2011-05-11 115 views
2

我需要通过dom4j中XML声明

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

字符串从dom4j的文件类型删除XML声明

我创建的文档解析,书籍DOC由DocumenHelper不包含XML声明(它来自XML => XSL => XML转换) 我认为DocumentHelper将声明添加到文档主体?

有没有办法从的

doc 

回答

2

您需要与根元素而不是文档进行交互。 例如,使用默认的,紧凑的OUTPUTFORMAT通过PhilW提到:

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

的简单的解决方案,我选择是

doc.getRootElement().asXML(); 
+0

非常简单,美观大方。这帮助了我! – user1506104 2017-12-14 07:11:30