Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete <?xml version="1.0" encoding="UTF-8" standalone="no"?> from xml document [duplicate]

Tags:

java

I wrote code for tests - it creates simple XML document. My problem is that created document contains unneeded data in first node . Is any way to delete or modify this node <?xml version="1.0" encoding="UTF-8" standalone="no"?> ?

import java.io.ByteArrayOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class MyTestClass {

    public static void main(String argv[]) {

      try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("company");
        doc.appendChild(rootElement);

        // staff elements
        Element staff = doc.createElement("Staff");
        rootElement.appendChild(staff);

        // firstname elements
        Element firstname = doc.createElement("firstname");
        firstname.appendChild(doc.createTextNode("yong"));
        staff.appendChild(firstname);

        // lastname elements
        Element lastname = doc.createElement("lastname");
        lastname.appendChild(doc.createTextNode("mook kim"));
        staff.appendChild(lastname);

        // nickname elements
        Element nickname = doc.createElement("nickname");
        nickname.appendChild(doc.createTextNode("mkyong"));
        staff.appendChild(nickname);

        // salary elements
        Element salary = doc.createElement("salary");
        salary.appendChild(doc.createTextNode("100000"));
        staff.appendChild(salary);

        Transformer t = TransformerFactory.newInstance().newTransformer();
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        t.transform(new DOMSource(doc),new StreamResult(s));
        System.out.println(new String(s.toByteArray()));
      } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
      } catch (TransformerException tfe) {
        tfe.printStackTrace();
      }
    }
}

It returns:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
 <company>  
  <Staff>
     <firstname>yong</firstname>
     <lastname>mook kim</lastname>
     <nickname>mkyong</nickname>
     <salary>100000</salary>   
  </Staff> 
 </company>

Is any way to delete or modify this node <?xml version="1.0" encoding="UTF-8" standalone="no"?> ?

like image 297
Tom Cruise Avatar asked Jan 06 '14 15:01

Tom Cruise


1 Answers

You can add the following to your Transformer

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

see here for details

like image 81
Paul Whelan Avatar answered Sep 27 '22 18:09

Paul Whelan