How to remove encoding=“UTF-8” standalone=“no” from xml Document object in Java

独自空忆成欢 提交于 2019-12-09 16:51:57

问题


I want to create XML in Java.

     DocumentBuilderFactory dbfac = DocumentBuilderFactory.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!


回答1:


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.




回答2:


Why do you need to remove an encoding? But..

doc.setXmlStandalone(true);

will erase standalone="no"




回答3:


transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

This would resolve your issue, verified at JDK 6



来源:https://stackoverflow.com/questions/7511400/how-to-remove-encoding-utf-8-standalone-no-from-xml-document-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!