I am trying to generate a sitemap.xml using JAXB, and the location attribute is being ignored (I want to generate the xsi:schemaLocation
attribute on my root element).
I want to generate an xml as follows:
<?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>
I have seen other questions: @xmlSchema annotation use with jaxb and How to generate the correct sitemap namespace using JAXB and Spring @ResponseBody in controller?
But neither of these solve the problem I am experiencing with the annotation.
I have the following package-info:
@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
However, my unit test:
@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)
}
generates a root element as follows:
<?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">
If I update the test to explicitly set the location using jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "location..")
then it will get populated - but I want to understand why it is not working with the annotation.