Steve
Steve

Reputation: 772

Changing the JAXB namespace

I have a legacy system to which we send data which is hardcoded to demand a specific XML namespace be used. They have published the XSD and I'm generating JAXB classes from it. But when I run the code, my XML comes out like this:

<ns3:field1>data</ns3:field1>

But the legacy system requires:

<custom:field1>data</custom:field1>

JAXB is generating 100% legal XML, but the legacy system cannot handle the namespace being different.

I cannot change the legacy system or the XSD.

Is there any better way to make the resulting XML come out the way I need besides doing String.replace("ns3:", "custom:")? I'm using Java 8.

Upvotes: 2

Views: 2059

Answers (1)

arifng
arifng

Reputation: 876

Write package-info.java -

@XmlSchema(
    namespace = "http://your-namespace-url",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns = {
            @XmlNs(prefix = "", namespaceURI = "http://your-namespace-url"),
            @XmlNs(prefix = "custom", namespaceURI = "http://your-namespace-url/other")
    })

As you don't define package-info, that's why it put that namespace prefix.

Upvotes: 2

Related Questions