codecamp

描述文件地址

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>
编码字符集
FRAGMENT
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

JAXB 简单转化案例

JAXB 之 Trang

简单XML生成——Marshaller数据源

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }