2

I need to generate XML files from XSD generated Java Classes.

Some of the fields in those Java Classes as Object instead of any concrete type, and thus warrant a xsi:type attribute in the generated XML file, which is fine.

What is NOT fine though is that along with that xsi:type, the full namespace definition is added (xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"), and makes the XML very unreadable.

To sum it up, here is what I generate now:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:RootTag xmlns:ns="https://example.com">
    <ns:SomeObjectField xsi:type="xs:boolean" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">true</ns:SomeObjectField>
    <ns:SomeOtherObjectField xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Some other value</ns:SomePtherObjectField>
</ns:RootTag>

And this is what I would like to generate:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:RootTag xmlns:ns="https://example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns:SomeObjectField xsi:type="xs:boolean">true</ns:SomeObjectField>
    <ns:SomeOtherObjectField xsi:type="xs:string">Some other value</ns:SomePtherObjectField>
</ns:RootTag>
Dino Prašo
  • 601
  • 9
  • 22

2 Answers2

2

I had the same issue. The solution assuming you are using the marshaller of the JAXBContext, you can set a property for your namespace or schema location property. In my case I needed a noSchemaLocation:

 jaxbMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "facturaComputarizadaEstandar.xsd");

You might need to set a different property for your specific case.

Ivan Aranibar
  • 2,226
  • 2
  • 18
  • 21
  • 1
    An empty `xsi:schemaLocation` should also do: `marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "");` – basin Aug 24 '20 at 09:01
1

You can explicitly declare xsi in package-info.java:

@javax.xml.bind.annotation.XmlSchema(
        xmlns = {
            @javax.xml.bind.annotation.XmlNs(
                    prefix = "ns",
                    namespaceURI = "https://example.com"),
            @javax.xml.bind.annotation.XmlNs(
                    prefix = "xsi",
                    namespaceURI = javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI) },
        namespace = "https://example.com",
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.foo;
  • prefix = "ns": <ns:RootTag>
  • prefix = "": <RootTag>

See JAXBContextImpl.xmlNsSet

NamespaceContextImpl.declareNsUri()

JAXBContextImpl.schemaLocation

In the older jaxb implementation 2.1 the @XmlNs is only used when generating a schema file and as a workaround you can add:

@XmlSeeAlso(DummyTypeWithinXsi.class)
public class RootTag ...
...
@XmlRootElement(namespace = javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
public class DummyTypeWithinXsi {
}

_

basin
  • 3,949
  • 2
  • 27
  • 63