描述文件地址

2018-12-23 22:59 更新

Marshaller.JAXB_SCHEMA_LOCATION

有時候,程序可能需要指定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的配置文件中,時常能見到這個屬性。

Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION

如果沒有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>
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號