Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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!

like image 211
VextoR Avatar asked Sep 22 '11 07:09

VextoR


2 Answers

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

doc.setXmlStandalone(true);

will erase standalone="no"

like image 176
Erik Kaju Avatar answered Oct 13 '22 12:10

Erik Kaju


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

This would resolve your issue, verified at JDK 6

like image 32
Nan chen Avatar answered Oct 13 '22 13:10

Nan chen