我必须在JAXB中将xml解析为字符串对象。但是如何为这个xml创建对象
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,我创建了一个类来映射创建对象的对象并在控制台中打印它。但是唐不知道我做错了什么。
@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: IllegalAnnotationExceptions计数
如何添加包装器anotations以将元素包装在其他标题内的单独标题和标题下。
答案 0 :(得分:1)
试试这个课程
@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;
}
答案 1 :(得分:0)
这对我有用
class Country {
@XmlElement
String name;
//...
@XmlElementWrapper(name="states")
List<State> state;
}
class State {
@XmlElement
String name;
//..
}