Java/XML/JAXBContext

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create JAXBContext with custom object

 

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
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 Person1Test {
  public static void main(String[] args) throws JAXBException {
    Person person = new Person();
    person.setFirstName("L");
    person.setLastName("H");
    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(person, System.out);
  }
}
@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);
  }
}





Marshal enum

 

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlRootElement;
public class CardOutput {
  public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(Card.class);
    context.createMarshaller().marshal(Card.CLUBS, System.out);
  }
}
@XmlRootElement
@XmlEnum(String.class)
enum Card {
  CLUBS, DIAMONDS, HEARTS, SPADES
}





Marshal object with JAXB

 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
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 PersonOutput {
  public static void main(String[] args) throws JAXBException {
    Person p = new Person();
    p.setFirstName("B");
    p.setLastName("H");
    PersonName pn = new PersonName();
    pn.setValue("L");
    p.setFriend(pn);
    JAXBContext context = JAXBContext.newInstance(Person.class);
    context.createMarshaller().marshal(p, System.out);
  }
}
@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;
  }
}





Unmarshall to JAXB Element

 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
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;
import javax.xml.transform.stream.StreamSource;
public class MisspelledPersonUnmarshaller {
  public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("person");
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement<Person> element = unmarshaller.unmarshal(new StreamSource("p.xml"),
        Person.class);
    Person person = element.getValue();
    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);
  }
}





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);
  }
}





Use Marshal Validation

 
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
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;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
public class MarshalValidation {
  public static void main(String[] args) throws Exception {
    Person p = new Person();
    p.setFirstName("B");
    p.setLastName("H");
    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("person.xsd"));
    marshaller.setSchema(schema);
    marshaller.setEventHandler(new ValidationEventHandler() {
      public boolean handleEvent(ValidationEvent event) {
        System.out.println(event);
        return false;
      }
    });
    marshaller.marshal(p, System.out);
  }
}
@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);
  }
}