Java: Marshalling Object -- Removing extra ns2 annotation in xml

user459811 picture user459811 · Aug 10, 2011 · Viewed 32.1k times · Source

I am trying to marshall data within an object into an xml file based on a defined schema. However when I print out the xml file, I recieve extra annotations on the xml tags. Is there any way to get rid of the extra namespace annotation (i.e. ns2)

This is an example of the xml I receive from marshalling.

<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns:ns2="http://www.something.com/something">
    <ns2:food>steak</ns2:food>
    <ns2:beverage>water</ns2:beverage>
</root>

What I want is something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns="http://www.something.com/something">
    <food>steak</food>
    <beverage>water</beverage>
</root>

This is what my Java code is doing:

            JAXBContext context = JAXBContext.newInstance("com.schema");
            JAXBElement<FoodSchema> element = new JAXBElement<FoodSchema>
                (new QName("FoodSchema"), Food.class, foodSchema);
            Marshaller marshaller = context.createMarshaller();
            OutputStream os = new FileOutputStream(object.getFilePath());
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
            marshaller.marshal(element, os);

Any help is much appreciated! Thanks!

Answer

bdoughan picture bdoughan · Aug 10, 2011

By adding a namespace URI ("http://www.something.com/something") to the QName used to construct the JAXB element, and leveraging the package level @XmlSchema annotation will get you the namespace qualification that you are looking for:

package-info

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

import javax.xml.bind.annotation.*;

Food

package forum7014746;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Food {

    private String food;
    private String beverage;

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    public String getBeverage() {
        return beverage;
    }

    public void setBeverage(String beverage) {
        this.beverage = beverage;
    }

}

Demo

package forum7014746;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(Food.class);

        Food foodSchema = new Food();
        foodSchema.setFood("steak");
        foodSchema.setBeverage("water");

        JAXBElement<Food> element = new JAXBElement<Food> (new QName("http://www.something.com/something","FoodSchema"), Food.class, foodSchema);

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(element, System.out);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FoodSchema xmlns="http://www.something.com/something">
    <beverage>water</beverage>
    <food>steak</food>
</FoodSchema>