Java Tutorial/XML/JAXB

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

Marshal Java object to a file

   <source lang="java">

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, new FileOutputStream("result.xml"));
 }

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

}</source>





Marshal Java object to xml and output to console

   <source lang="java">

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

}</source>





The xjc Tool

The xjc tool accepts an XML schema and generates Java classes.

The generated classes contain properties mapped to the XML elements and attributes defined in the schema.

The xjc tool is a part of the Java API for XML Binding (JAXB) API.

The syntax to use the xjc tool is:



   <source lang="java">

xjc [-options ...] <schema_file/URL/dir> ... [-b <bindinfo>] ...</source>



OptionDescription-extensionAllows vendor extensions.-b <file>Specifies external binding files.-d <dir>Specifies the directory to store generated files.-p <pkg>Specifies the target package.-classpath <arg>Specifies where to find user class files.-quietSuppresses compiler output.-helpDisplays help information.


XMLSchema Mapping Annotations

  1. XmlAccessorOrder controls ordering of fields and properties for a class
  2. XmlAccessorType is used in conjunction with the XmlAccessType Enum to indicate if a field or property should be serialized
  3. XmlAnyAttribute acts as a map of wildcard attributes for java.util.Map properties or fields
  4. XmlAnyElement serves to identify the catchall property during unmarshalling
  5. XmlAttachmentRef identifies mime types and URIs for external content
  6. XmlAttribute renames of a JavaBeans property to/from an XML attribute
  7. XmlElement maps a JavaBeans property to a complex type
  8. XmlElementDecl links an object factory to an XML element
  9. XmlElementRef links a JavaBeans property to an XML element derived from the property"s type
  10. XmlElementRefs marks a property that refers to classes with @XmlElement
  11. XmlElements is a container for multiple @XmlElement annotations
  12. XmlElementWrapper generates a wrapper element for XML representation
  13. XmlEnum maps an Enum to an XML representation
  14. XmlEnumValue identifies an enumerated constant
  15. XmlID maps a property to an XML ID
  16. XmlIDREF maps a property to an XML IDREF
  17. XmlInlineBinaryData causes XOP encoding to be disabled for binary data types, such as Image
  18. XmlList maps a property to a list
  19. XmlMimeType identifies a textual representation of the mime type for a property
  20. XmlMixed identifies a multivalued property with mixed content
  21. XmlNs identifies an XML namespace
  22. XmlRegistry marks a class that has @XmlElementDecl
  23. XmlRootElement maps a class or enumeration to an XML element
  24. XmlSchema identifies a target namespace for a package
  25. XmlSchemaType maps a Java type to a built-in schema type
  26. XmlSchemaTypes acts as a container for multiple @XmlSchemaType annotations
  27. XmlTransient sets a property that shouldn"t be saved
  28. XmlType maps a class or enumeration to a schema type
  29. XmlValue maps of a class to a simple schema content or type