Link to home
Create AccountLog 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
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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?