2

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.

2 Answers 2

1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.