Issue with namespaces in XMLs in JAXB unmarshalling

前端 未结 1 1418
遇见更好的自我
遇见更好的自我 2021-01-25 04:11

I have an XML to unmarshall with JAXB. The code works fine if I remove all namespace attributes from the elements but I get a null object after unmarshalling if I keep the names

1条回答
  •  无人共我
    2021-01-25 04:50

    XMLAtribute has atribute namesape, so

    @XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization").
    

    While judging by your xml, the cat is in the same namespace as the animal so

    The following code works with JDK 7 (fixed the ns for animal and attribute name for zid).

    @XmlRootElement(name = "Animal",namespace = "http://allmycats.com/serviceplatform/1.0/")
    public class Animal2{
        List cats;
    
        @XmlElement(name = "Cat")
        public List getCats() {
            return cats;
        }
    
        public void setCats(Listcats) {
            this.cats= cats;
        }
    }
    @XmlRootElement(name = "Cat")
    public class Cat2 {
        private String zId;
    
        @XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
        public String getzId() {
            return zId;
        }
        public void setzId(String zId) {
            this.zId = zId;
        }
    
        private String name;
    
        @XmlElement(name = "name")
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    

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