I found here how I can override the print-out of the XML document to my Eclipse console so that it includes standalone = "no", but how do I write standalone = "no" to a file? I have tried writing the same document to a file, and it still will not print standalone = "no". In other words, when I try writing to a file, the overridden method does not work.
Is there some other method that I should override when writing to a file? What is the issue here?
private static void writeXML() {
try {
Document doc = new Document();
Element theRoot = new Element("tvshows");
doc.setRootElement(theRoot);
Element show = new Element("show");
Element name = new Element("name");
name.setAttribute("show_id", "show_001");
name.addContent(new Text("Life on Mars"));
Element network = new Element("network");
network.setAttribute("country", "US");
network.addContent(new Text("ABC"));
show.addContent(name);
show.addContent(network);
theRoot.addContent(show);
//-----------------------------
Element show2 = new Element("show");
Element name2 = new Element("name");
name2.setAttribute("show_id", "show_002");
name2.addContent(new Text("Life on Mars"));
Element network2 = new Element("network");
network2.setAttribute("country", "UK");
network2.addContent(new Text("BBC"));
show2.addContent(name2);
show2.addContent(network2);
theRoot.addContent(show2);
XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat(), XMLOUTPUT);
//xmlOutput.output(doc, System.out);
xmlOutput.output(doc, new FileOutputStream(new File("./src/jdomMade.xml")));
System.out.println("The file has been written");
}
catch (Exception ex){
ex.printStackTrace();
}
}
public static final XMLOutputProcessor XMLOUTPUT = new AbstractXMLOutputProcessor() {
@Override
protected void printDeclaration(final Writer out, final FormatStack fstack) throws IOException {
write(out, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?> ");
write(out, fstack.getLineSeparator());
}
};