W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
有時候,程序可能需要指定xsi:schemaLocation
,則可以添加屬性JAXB_SCHEMA_LOCATION
:
@Test
public void test3() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd");
marshaller.marshal(one, System.out);
}
得到的結(jié)果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<one xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<name>Test one</name>
</one>
xsi:schemaLocation
其實(shí)是Namespace為"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"里的schemaLocation屬性,它定義了XML Namespace和對應(yīng)的XSD(Xml Schema Definition)文檔的位置的關(guān)系。在Spring的配置文件中,時常能見到這個屬性。
如果沒有Namespeace,但是需要使用Schema,就需要用到JAXB_NO_NAMESPACE_SCHEMA_LOCATION
:
@Test
public void test4() throws JAXBException {
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "ehcache.xsd");
marshaller.marshal(one, System.out);
}
得到的結(jié)果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<one xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<name>Test one</name>
</one>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: