Consider the following code:
Main.java
====
package com.sample;
import com.sample.entity.Customer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Main {
public static void main(String[] args) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Customer customer = new Customer();
customer.setId(123);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(customer, System.out);
}
}
Customer.java
====
package com.sample.entity;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customer {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
package-info.java
====
@XmlSchema(namespace = "http://www.example.org/package", elementFormDefault = XmlNsForm.QUALIFIED)
package com.sample.entity;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;
I wish to reuse the Customer POJO and create two different namespace values. So first I wish to print the output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer xmlns="http://www.example.org/package">
<id>123</id>
</customer>
like the current code does, then create a second xml with a different main namespace from the same pojo that will look like this
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer xmlns="http://www.another.org/package">
<id>123</id>
</customer>
or remove the namespace all together
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
<id>123</id>
</customer>