1

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());
}

};
Community
  • 1
  • 1
Raisintoe
  • 201
  • 1
  • 4
  • 11
  • possible duplicate of [create xml with jdom, how to set standalone="no" attribute](http://stackoverflow.com/questions/21170732/create-xml-with-jdom-how-to-set-standalone-no-attribute) – rolfl May 08 '15 at 03:48
  • Hmmm. Apon reading more carefully, your issue is not about the standalone, but about writing it to a file. What have you tried? Can you copy your code here? – rolfl May 08 '15 at 03:50
  • I ran the code using a Writer, and an OutputStream, works for me. – rolfl May 08 '15 at 03:57
  • See pastebin here: http://pastebin.com/ni8ZMQ6e – rolfl May 08 '15 at 04:01
  • OK, I added in my code. When sending to System.out, [xmlOutput.output(doc, System.out);], the printDeclaration() method is overridden, but when streaming to a file, xmlOutput.output(doc, new FileOutputStream(new File("./src/jdomMade.xml")));, it is not overridden. – Raisintoe May 08 '15 at 04:33

1 Answers1

1

Your code lies ;-)

xmlOutput.output(doc, new FileOutputStream(new File("./src/jdomMade.xml")));

System.out.println("The file has been written");

The println says the file has been written, but it has not.

The File is only written when the file is flushed, and closed. You do not do that.

You should add a try-with-resources to your code:

try (FileOutputStream fos = new FileOutputStream(new File("./src/jdomMade.xml"))) {
    xmlOutput.output(doc, fos);
}

System.out.println("The file has been written");
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • I look at the file, it updates whenever I write to it. – Raisintoe May 08 '15 at 05:14
  • @Raisintoe - I have copied your code, run it, etc. It all works for me. The only issue I see is the lack of flush/close. Are you sure it is updating? Add a change that's new each time your run it.... Add a timestamp, or unique number.... and make sure it matches the value you put in the println. – rolfl May 08 '15 at 05:34
  • Ha ha, I found the problem! I was opening the file with Internet Explorer. It automatically removes the [standalone = "no"]. I just opened the file with notepad, and there it was! Geez. Thank you for your help. – Raisintoe May 08 '15 at 05:42
  • Well, thanks, I think. Today I learned something new - that Internet Explorer lies too ;-) – rolfl May 08 '15 at 05:52