Java by API/javax.xml.bind/Marshaller

Материал из Java эксперт
Перейти к: навигация, поиск

Marshaller.JAXB_FORMATTED_OUTPUT

  

import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
public class JavaToXMLDemo {
  public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Employee.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Cath");
    object.setSalary(300);
    m.marshal(object, System.out);
  }
}
@XmlRootElement
class Employee {
  private String code;
  private String name;
  private int salary;
  public String getCode() {
    return code;
  }
  public void setCode(String code) {
    this.code = code;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getSalary() {
    return salary;
  }
  public void setSalary(int population) {
    this.salary = population;
  }
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <code>CA</code>
    <name>Cath</name>
    <salary>300</salary>
</employee>





Marshaller.marshal(Object jaxbElement, OutputStream os) throws JAXBException

  
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Weird {
    
    private String value;
    
    @XmlAttribute(name="value")
    private String svalue;
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public static void main(String[] args) throws JAXBException {
        Weird w = new  Weird();
        w.value="foo";
        w.svalue="bar";
        JAXBContext context = JAXBContext.newInstance(Weird.class);
        context.createMarshaller().marshal(w, System.out);
    }
}





Marshaller: setEventHandler(ValidationEventHandler handler) throws JAXBException

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





Marshaller: setProperty(String name, Object value)

  

import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
public class JavaToXMLDemo {
  public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Employee.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Cath");
    object.setSalary(300);
    m.marshal(object, System.out);
  }
}
@XmlRootElement
class Employee {
  private String code;
  private String name;
  private int salary;
  public String getCode() {
    return code;
  }
  public void setCode(String code) {
    this.code = code;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getSalary() {
    return salary;
  }
  public void setSalary(int population) {
    this.salary = population;
  }
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <code>CA</code>
    <name>Cath</name>
    <salary>300</salary>
</employee>