Reputation: 291
I have to parse a xml into string objects in JAXB. But how to creates objects for this 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>
And to parse this xml I have created class to map the objects which creates the objects and print it in the console. But Don't know what I am doing wrong.
@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;
}
when I run the program I m getting the
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: counts of IllegalAnnotationExceptions
How to add wrapper anotations to wrapping the elements under separate headers and headers inside other headers.
Upvotes: 2
Views: 182
Reputation: 1445
Try this 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;
}
Upvotes: 1
Reputation: 136002
This worked for me
class Country {
@XmlElement
String name;
//...
@XmlElementWrapper(name="states")
List<State> state;
}
class State {
@XmlElement
String name;
//..
}
Upvotes: 0