简体   繁体   English

dom4j文档中的XML声明

[英]dom4j XML declaration in document

I need to remove XML declaration from dom4j document type 我需要从dom4j文档类型中删除XML声明

I am creating document by 我正在创建文档

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

String parsed to Document doc by DocumenHelper contains no XML declaration (it comes from XML => XSL => XML transformation) I think that DocumentHelper is adding declaration to a document body ? 由DocumenHelper解析为Document doc的字符串不包含XML声明(它来自XML => XSL => XML转换)我认为DocumentHelper正在向文档正文添加声明?

Is there any way to remove XML declaration from the body of 有没有办法从正文中删除XML声明

doc

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

doc.getRootElement().asXML();

You need to interact with the root element instead of the document. 您需要与根元素而不是文档进行交互。 For example, using the default, compact OutputFormat mentioned by PhilW : 例如,使用PhilW提到的默认的紧凑OutputFormat:

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

I'm not sure where exactly this the declaration is a problem in your code. 我不确定这个声明在你的代码中究竟是什么问题。 I had this once when I wanted to write an xml file without declaration (using dom4j). 当我想写一个没有声明的xml文件(使用dom4j)时,我曾经有过这个。

So if this is your use case: "omit declaration" is what you're looking for. 所以如果这是你的用例:“省略声明”就是你要找的东西。 http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/OutputFormat.html http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/OutputFormat.html

Google says this can be set as a property as well, not sure what it does though. 谷歌表示,这也可以设置为一个属性,但不确定它的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM