W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
JAXB_FRAGMENT
是一個多面手,在不同的輸出場景下,表現(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
,表明結果不再聲明XML頭信息,得到的結果:
<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());
}
輸出結果:
startDocument
endDocument
如果將注釋的一行代碼放開,再次運行程序將不能得到任何輸出。JAXB_FRAGMENT
的默認值為false
,在其他場景下也有不同的表現(xiàn)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: