简体   繁体   中英

Jaxb marshalling to wrong character

I used to marshall my models by calling there toXml():

@XmlRootElement
public class MyModel {

    private String body;

    public String getBody() {
        return this.body;
    }

    public void setBody(final String _body) {
        this.body = _body;
    }

    public String toXML() throws JAXBException {
        final JAXBContext context = JAXBContext.newInstance(MyModel.class);
        final Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        final StringWriter writer = new StringWriter();
        marshaller.marshal(this, writer);
        return writer.toString();
    }
}

If I write this to a file or via a stream to a webdav-target, it leads to an invalid xml when using ie ä as a textvalue.

final MyModel m = new MyModel();
m.setBody("\u00E4");

final FileWriter w = new FileWriter("D:\\outtest.xml");
w.write(m.toXML());
w.close();

The XMl is something like this (looks other in npp and is correct there, when switching to ansi):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myModel><body>伯</body></myModel>

I need a hint to solve this!


OK, this works as it should (Thx!):

final MyModel m = new MyModel();
m.setBody("\u00E4");
final FileOutputStream s = new FileOutputStream("D:\\outtest.xml");
final OutputStreamWriter osw = new OutputStreamWriter(s, Charset.forName("UTF-8"));
osw.write(m.toXML());
osw.close();

My problem now is, that this file-testing was the simplified version ;-)
I call toXml() and write this string an JPA-moodel. Later this should be written to webdav using a framework-class whichs takes an ByteArrayInputStream as input. So I use this code:

final ByteArrayInputStream stream = new ByteArrayInputStream(jpaModel.getXmlString().getBytes());

...and writing this I finally got it. Using the 'more detailed' getBytes(Charset.forName("UTF-8")) helps me out! Thanks

Okay, now we know how you're writing the file, this is the problem:

FileWriter w = new FileWriter("D:\\outtest.xml");

That will always use the platform-default encoding - even though you've told JAXB that you're going to be using UTF-8. It's a bit like creating the data for a JPEG, but then saving it as a .png file. Use a FileOutputStream wrapped in an OutputStreamWriter using UTF-8, and all should be well.

If you're using Java 7, you can simplify this:

try (Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
    writer.write(m.toXML());
}

Alternatively, you might want to change your toXML method to accept an OutputStream to write the data to, instead of returning a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM