How to remove encoding="UTF-8" standalone="no" from xml Document object in Java
I want to create XML in Java.
DocumentBuilderFactory dbfac = DocumentBuil开发者_高级运维derFactory.newInstance();
DocumentBuilder docBuilder;
docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
but Java automatically creates declaration like this
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
How can I remove encoding="UTF-8" standalone="no"
so it will be
<?xml version="1.0"?>
Thanks!
Why do you need to remove an encoding? But..
doc.setXmlStandalone(true);
will erase standalone="no"
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
This would resolve your issue, verified at JDK 6
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.
I think this is a good way.
精彩评论