Java by API/javax.xml.bind.annotation/XmlRootElement

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

javax.xml.bind.annotation.XmlRootElement

  

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>





XmlRootElement.name()

  
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "person", namespace = "http://www.example.ru/myperson")
public class MyPerson {
  private String first;
  private String last;
  public static void main(String[] args) throws JAXBException {
    MyPerson p = new MyPerson();
    p.first = "l";
    p.last = "h";
    JAXBContext context = JAXBContext.newInstance(MyPerson.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(p, System.out);
  }
  public String getFirst() {
    return first;
  }
  public void setFirst(String first) {
    this.first = first;
  }
  public String getLast() {
    return last;
  }
  public void setLast(String last) {
    this.last = last;
  }
}