使用 JAXB 将 XML 数据解析为字符串 objects

Parsing XML data into string objects using JAXB

我必须在 JAXB 中将 xml 解析为字符串 objects。但是如何为此 xml

创建 objects

Country.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <Country>       
        <name>India</name>
        <capital>New Delhi</capital>
        <population>120crores</population>
    .
    .
    .
    .
    .

<states>
    <state>
        <name>Maharastra</name>
        <pincode>xyzzzz</pincode>
        <capital>Mumbai</capital>

    <\state>

    <state>
    .
    .
    .
    </state>
</states>


   <\Country>       

为了解析这个 xml,我创建了 class 来映射 objects,它创建了 objects 并将其打印在控制台中。但是不知道我做错了什么。

@XmlElementWrapper(name="Country")
    public void setCountry(String Countryv) {
        Country= Countryv;
    }

@XmlElement (name = "name")
    public void setname(String namev) {
        name= namev;
    }

    @XmlElement (name = "capital")
    public void setcapital(String capitalv) {
        capital= capitalv;
    }

    @XmlElement (name = "population")
    public void setpopulation(String populationv) {
        population= populationv;
    }


    @XmlElementWrapper(name="states")
    public void setType(String statesv) {
        states = statesv;
    }



@XmlElementWrapper(name="state")
        public void setType(String statev) {
            state = statev;
        }




    @XmlElement (name = "name")
        public void setpopulation(String namev) {
            name= namev;
        }

     @XmlElement (name = "pincode")
        public void setpopulation(String pincodev) {
            pincode= pincodev;
        }

 @XmlElement (name = "capital")
        public void setpopulation(String capitalv) {
            capital= capitalv;
        }

当我 运行 程序时,我得到

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: counts of IllegalAnnotationExceptions

如何添加包装器注释以将单独的 headers 和 headers 下的元素包装在其他 headers.

试试这个 class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "name",
    "capital",
    "population",
    "states"
})
@XmlRootElement(name = "Country")
public class Country {
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String capital;
@XmlElement(required = true)
protected String population;
@XmlElement(required = true)
protected Country.States states;
    public String getName() {
        return name;
    }
    public void setName(String value) {
        this.name = value;
    }
    public String getCapital() {
        return capital;
    }
    public void setCapital(String value) {
        this.capital = value;
    }
    public String getPopulation() {
        return population;
    }
    public void setPopulation(String value) {
        this.population = value;
    }
    public Country.States getStates() {
        return states;
    }
    public void setStates(Country.States value) {
        this.states = value;
    }

这对我有用

class Country {
    @XmlElement
    String name;
    //...
    @XmlElementWrapper(name="states")
    List<State> state;
}

class State {
    @XmlElement
    String name;
    //..
}