Source Code : Unmarshall with JAXB
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Unmarshall with JAXB
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
public class PersonUnmarshaller {
public static void main(String[] args) throws JAXBException {
JAXBContext context = JAXBContext.newInstance("person");
Unmarshaller unmarshaller = context.createUnmarshaller();
Person person = (Person) unmarshaller.unmarshal(new File("person.xml"));
System.out.println(person.getFirstName());
}
}
@XmlRootElement()
@XmlType(name = "")
// @XmlAccessorType(XmlAccessType.FIELD)
class Person {
// @XmlAttribute()
private String firstName;
private PersonName friend;
private String lastName;
public String getFirstName() {
return firstName;
}
@XmlElement(nillable = true)
// @XmlElement(required=true)
public PersonName getFriend() {
return friend;
}
// @XmlValue()
// @XmlTransient
public String getLastName() {
return lastName;
}
public void setFirstName(String s) {
firstName = s;
}
public void setFriend(PersonName friend) {
this.friend = friend;
}
// r @XmlTransient
public void setLastName(String s) {
lastName = s;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
class PersonName {
@XmlValue
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public static void main(String[] args) throws JAXBException {
PersonName pn = new PersonName();
pn.value = "foo";
JAXBContext context = JAXBContext.newInstance(PersonName.class);
context.createMarshaller().marshal(pn, System.out);
}
}
Thank with us