JAXB_FRAGMENT
是一個(gè)多面手,在不同的輸出場(chǎng)景下,表現(xiàn)出不同的效果。
@Test
public void test5_1() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(one, System.out);
}
在這里指定JAXB_FRAGMENT=true
,表明結(jié)果不再聲明XML頭信息,得到的結(jié)果:
<one>
<name>Test one</name>
</one>
可以看到,第一行的XML聲明不見了。
如果使用到了SAX方式(見下一小節(jié)),可以發(fā)現(xiàn)如下不同:
@Test
public void test5_2() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(one, new MyContentHandler());
}
輸出結(jié)果:
startDocument
endDocument
如果將注釋的一行代碼放開,再次運(yùn)行程序?qū)⒉荒艿玫饺魏屋敵觥?code>JAXB_FRAGMENT的默認(rèn)值為false
,在其他場(chǎng)景下也有不同的表現(xiàn)。
更多建議: