Java: Marshalling Object — Removing extra ns2 annotation in xml

前端 未结 3 551
长发绾君心
长发绾君心 2020-12-06 05:13

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.

相关标签:
3条回答
  • 2020-12-06 05:43

    add to xsd schema definition elementFormDefault and attributeFormDefault:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ...
    elementFormDefault="qualified" attributeFormDefault="unqualified" ... >
    
    0 讨论(0)
  • 2020-12-06 05:45

    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>
    
    0 讨论(0)
  • 2020-12-06 05:45

    Thanks for answer. Just to give sample code before:

     marshaller.marshal(new  JAXBElement(new QName(**""**,"Document"),Document.class,swiftDoc), sw);
    

    after:

    marshaller.marshal(new  JAXBElement(new QName(**"http://www.w3.org/2001/XMLSchema**","Document"),Document.class,swiftDoc), sw);
    

    and extra "ns:" was removed.

    0 讨论(0)
提交回复
热议问题