我试图使用JAXB生成一个sitemap.xml,并且忽略了location属性(我想在根元素上生成xsi:schemaLocation属性)。
我想生成xml,如下所示:
<?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命名空间?
但这两种方法都不能解决我在注释中遇到的问题。
我有以下信息:
@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但是,我的单元测试:
@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)
}生成根元素如下:
<?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..")显式设置位置,那么它将被填充-但是我想了解为什么它不使用注释。
发布于 2015-03-23 19:45:03
根据规格说明
请注意执行者..。 但是,允许模式生成器在schemaLocation属性中使用不同的值(包括不生成这样的属性),这样用户就可以通过命令行接口指定资源的本地副本。
因此,是否生成位置取决于实现提供程序。
https://stackoverflow.com/questions/29217397
复制相似问题