The code for writing an XML document using XMLOutputStream is given below:

Java Code:
try {
    ObjectOutput os = new XMLOutputStream(System.out);
    os.writeObject(new JButton("Press Me"});
    os.close();
} catch(Exception e) {
    e.printStackTrace();
}