如何从 Java 中的 xml Document 对象中删除 encoding="UTF-8" standalone="no"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7511400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove encoding="UTF-8" standalone="no" from xml Document object in Java
提问by VextoR
I want to create XML in Java.
我想用 Java 创建 XML。
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
but Java automatically creates declaration like this
但是Java会自动创建这样的声明
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
How can I remove encoding="UTF-8" standalone="no"
so it will be
我怎样才能删除 encoding="UTF-8" standalone="no"
它
<?xml version="1.0"?>
Thanks!
谢谢!
采纳答案by Igor Konoplyanko
I think there is no legal way to exclude theese attributes from generation. But after it's generated you can use XSLT to remove this.
我认为没有合法的方法可以从世代中排除这些属性。但是在它生成之后,您可以使用 XSLT 来删除它。
I think this is a good way.
我认为这是一个好方法。
回答by Erik Kaju
Why do you need to remove an encoding? But..
为什么需要删除编码?但..
doc.setXmlStandalone(true);
will erase standalone="no"
会抹掉 standalone="no"
回答by Nan chen
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
This would resolve your issue, verified at JDK 6
这将解决您的问题,已在 JDK 6 中验证