编码字符集
Marshaller.JAXB_ENCODING
如果需要改输出XML的字符编码,可以添加属性JAXB_ENCODING
:
@Test
public void test2() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
marshaller.marshal(one, System.out);
}
得到的结果:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<one>
<name>Test one</name>
</one>
已经不再是默认的UTF-8
。