Link to home
Start Free TrialLog in
Avatar of cgray1223
cgray1223

asked on

Use JAXB to create Object from XML String

Hello,

How can I use the below code to unmarshal a XML string an map it to the JAXB object below?
 

JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Person person = (Person) unmarshaller.unmarshal("xml string here");

Open in new window



@XmlRootElement(name = "Person")
public class Person {
    @XmlElement(name = "First-Name")
    String firstName;
    @XmlElement(name = "Last-Name")
    String lastName;
    public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure what you are asking? The code that you posted will in fact unmarshal the string into the person object. Are you asking about what libraries, etc in addition to this will you need to make it run? If so, let us know a bit more about your environment. For example, are you using spring, maven, is it a web-app, etc?