Java by API/javax.xml.bind/JAXBContext

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

JAXBContext: createMarshaller() 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);
    }
}





JAXBContext: createUnmarshaller()

  

import generated.Item;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class UnmarshallingDemo {
    public static void main (String [] args) {
        try {
            JAXBContext jc = JAXBContext.newInstance ("generated");
            Unmarshaller u = jc.createUnmarshaller ();
           File f = new File ("item.xml");
           JAXBElement element = (JAXBElement) u.unmarshal (f);
           Item item = (Item) element.getValue ();
           System.out.println (item.getCode ());
           System.out.println (item.getName ());
           System.out.println (item.getPrice ());
       } catch (JAXBException e) {
           e.printStackTrace ();
       }
   }
}

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="item" type="Item"/>
    <xsd:complexType name="Item">
        <xsd:sequence>
            <xsd:element name="code" type="xsd:string"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="price" type="xsd:double"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>





JAXBContext.newInstance(Class... classesToBeBound)

  

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>