描述文件地址

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);
    }

得到的结果:

<?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其实是Namespace为"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"里的schemaLocation属性,它定义了XML Namespace和对应的XSD(Xml Schema Definition)文档的位置的关系。在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);
    }

得到的结果:

<?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>
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号