How to remove encoding = "UTF-8" standalone = "no" from xml Document Object in Java

I want to create XML in Java.

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); 

but Java automatically creates an ad like this

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 

How to remove encoding="UTF-8" standalone="no" so that it is

 <?xml version="1.0"?> 

Thanks!

+6
source share
3 answers

I think there is no legal way to exclude these attributes from the generation. But after creating it, you can use XSLT to remove this.

I think this is a good way.

-2
source

Why do you need to delete the encoding? But..

 doc.setXmlStandalone(true); 

erases standalone="no"

+13
source
 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 

This will solve your problem confirmed in JDK 6

+11
source

Source: https://habr.com/ru/post/897805/


All Articles