taygetos
taygetos

Reputation: 3040

JAXB unmarshaling with namespaces, but without prefix

I want to use JAXB to

  1. unmarshal an xml into a java object,
  2. marshal it back to an xml
  3. and produce the exact same output with namespaces

Here is the original XML:

<customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.com">
    <dataList>
        <data xsi:type="keyValuePair">
            <key>key1</key>
            <value>val1</value>
        </data>
        <data xsi:type="keyValuePair">
            <key>key2</key>
            <value>val2</value>
        </data>
    </dataList>
    <id>123</id>
</customer>

I am using the following POJOs:

@XmlRootElement(namespace = "http://example.com")
public class Customer {

  private String id;
  private List<KeyValueData> data;

  public Customer(){}

  @XmlElement
  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  @XmlElement
  @XmlElementWrapper(name="dataList")
  public List<KeyValueData> getData() {
    return data;
  }

  public void setData(List<KeyValueData> data) {
    this.data = data;
  }
}

public class KeyValueData {

  private String type;
  private String key;
  private String value;

  public KeyValueData(){}

  @XmlElement
  public String getKey() {
    return key;
  }

  public void setKey(String key) {
    this.key = key;
  }

  @XmlElement
  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  @XmlAttribute(namespace = "http://www.w3.org/2001/XMLSchema-instance" )
  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

}

The code i am using for un/marshalling:

InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
JAXBContext context = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(inputStream);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(customer, System.out);

And this is not really working, i cannot even unmarshal the initial xml. But if i try just marshaling a POJO where i just set the fields in the code. I get the following result:

<ns3:customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="http://example.com">
  <dataList>
    <data xsi:type="keyValuePair">
      <key>key1</key>
      <value>val1</value>
    </data>
    <data xsi:type="keyValuePair">
      <key>key2</key>
      <value>val2</value>
    </data>
  </dataList>
  <id>123</id>
</ns3:customer>

How do i get rid of this ns3 prefix?

Upvotes: 1

Views: 691

Answers (1)

taygetos
taygetos

Reputation: 3040

I found a solution by adding the namespace definition in the package level in a

package-info.java

@XmlSchema(namespace = "http://example.com", elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;

(And remove it from the @XMLRootElement)

Upvotes: 1

Related Questions