首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略JAXB @XmlSchema位置

忽略JAXB @XmlSchema位置
EN

Stack Overflow用户
提问于 2015-03-23 18:10:35
回答 1查看 1.6K关注 0票数 1

我试图使用JAXB生成一个sitemap.xml,并且忽略了location属性(我想在根元素上生成xsi:schemaLocation属性)。

我想生成xml,如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<ns3:urlset 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ns3="http://www.sitemaps.org/schemas/sitemap/0.9"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <urls>
        <loc>http://domain.com</loc>
    </urls>
    <urls>
        <loc>http://domain.com/test</loc>
    </urls>
</ns3:urlset>

我见过其他问题:@xmlSchema注释与jaxb一起使用如何使用JAXB和Spring @ResponseBody在控制器中生成正确的sitemap命名空间?

但这两种方法都不能解决我在注释中遇到的问题。

我有以下信息:

代码语言:javascript
复制
@javax.xml.bind.annotation.XmlSchema(
    namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
    xmlns = @javax.xml.bind.annotation.XmlNs( prefix = "xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance" ),
    location = "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED
)
package com.domain.site.sitemap

但是,我的单元测试:

代码语言:javascript
复制
@Test public void createXmlObject(){
    List urls = [ "test1", "test2", "test3" ]
    Sitemap map = new Sitemap( urls )
    JAXBContext jaxbContext = JAXBContext.newInstance( Sitemap )
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller()
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
    jaxbMarshaller.marshal(map, System.out)
}

生成根元素如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="http://www.sitemaps.org/schemas/sitemap/0.9">

如果我将测试更新为使用jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "location..")显式设置位置,那么它将被填充-但是我想了解为什么它不使用注释。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-23 19:45:03

根据规格说明

请注意执行者..。 但是,允许模式生成器在schemaLocation属性中使用不同的值(包括不生成这样的属性),这样用户就可以通过命令行接口指定资源的本地副本。

因此,是否生成位置取决于实现提供程序。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29217397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档