Reputation: 354
I'm using Java 8 and JDOM 2.0.6 (Mac-Yosemite + Eclipse) to generate an XML file.
The prolog of the file comes out with these bytes preceding <?xml
C2 A8 C3 8C
I'm using XMLOutputter.output() to write out the Document. When I direct the output to console, it comes out correctly. When directed output to a file, I get the errant bytes inserted.
relevant code: `
private Document outputDoc = new Document();
outputDoc.setRootElement(new Element("GraphicalAlgorithm_" + challengeID, DFG2D_NAMESPACE));
outputDoc.getRootElement().addContent(..my Element...);
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
//TEST ONLY: writes to console
xmlOutputter.output(outputDoc, System.out);
xmlOutputter.output(outputDoc, fileStream);;
`
I'm stumped on this one.
Upvotes: 0
Views: 44
Reputation: 354
I stepped into this minefield by copying and pasting the "file output" method I had been previously using for Serialization (.ser) file output.
The errant 4-bytes are a "magic ID" that Java serialization stamps into a FileOutpuStream which has been previously attached to an ObjectOutputStream (the specialized outputter you use for serialized output calls, e.g. "writeObject(obj)". One might assume that you have to actually invoke "writeOutput(obj)" for the serializer to mark the file this way, but no.
A complete analysis/write-up and repair can be found here:
https://github.com/hunterhacker/jdom/issues/193
Upvotes: 0