Reputation: 21
I have some problems by opening/ loading a generated xml-file because it's not well formed ("Invalid byte 1 of 1-byte UTF-8 sequence"). I already searched for some solutions and changed my code for getting the design as following:
System.out.println("Which Design-File shall be modified? Please enter: [file].xml");
String file = "file.xml";
// generate JDOM Document
SAXBuilder builder = new SAXBuilder();
try {
// getting rid of the UTF8 problem when reading the modified xml file
InputStream inputStream= new FileInputStream(file)
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
Document doc = builder.build(is);
}
catch (JDOMException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
after changing the design, I write it with the XMLOutputter as followed:
String fileNew = "file_modified.xml";
FileWriter writer;
try {
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
writer = new FileWriter(fileNew);
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc, writer);
} catch (IOException e) {
e.printStackTrace();
}
Does somebody has a solution for my problem? I really thought that these codelines would be solve my problem, but when I load it into the firefox it still says that it's not well formed :/.
Upvotes: 2
Views: 484
Reputation: 17707
From the XMLOutputter documentation:
Warning: When outputting to a
Writer
, make sure the writer's encoding matches the encoding setting in the Format object. This ensures the encoding in which the content is written (controlled by theWriter
configuration) matches the encoding placed in the document's XML declaration (controlled by theXMLOutputter
). Because aWriter
cannot be queried for its encoding, the information must be passed to the Format manually in its constructor or via theFormat.setEncoding(java.lang.String)
method. The default encoding is UTF-8.
JDOM uses the encoding of the Format instance to determine the encoding used for OutputStreams though, so, the recommended way to write your code is:
try (FileOutputStream fos = new FileOutputStream(fileNew)) {
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc, fos);
} catch (IOException e) {
e.printStackTrace();
}
Using OutputStreams instead of Writers will ensure that the actual encoding used is applied to the file, instead of the platform default.
Upvotes: 1