Java Tutorial/XML/JAXB
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Marshal Java object to a file
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;
}
}
Marshal Java object to xml and output to console
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;
}
}
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:
xjc [-options ...] <schema_file/URL/dir> ... [-b <bindinfo>] ...
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
- XmlAccessorOrder controls ordering of fields and properties for a class
- XmlAccessorType is used in conjunction with the XmlAccessType Enum to indicate if a field or property should be serialized
- XmlAnyAttribute acts as a map of wildcard attributes for java.util.Map properties or fields
- XmlAnyElement serves to identify the catchall property during unmarshalling
- XmlAttachmentRef identifies mime types and URIs for external content
- XmlAttribute renames of a JavaBeans property to/from an XML attribute
- XmlElement maps a JavaBeans property to a complex type
- XmlElementDecl links an object factory to an XML element
- XmlElementRef links a JavaBeans property to an XML element derived from the property"s type
- XmlElementRefs marks a property that refers to classes with @XmlElement
- XmlElements is a container for multiple @XmlElement annotations
- XmlElementWrapper generates a wrapper element for XML representation
- XmlEnum maps an Enum to an XML representation
- XmlEnumValue identifies an enumerated constant
- XmlID maps a property to an XML ID
- XmlIDREF maps a property to an XML IDREF
- XmlInlineBinaryData causes XOP encoding to be disabled for binary data types, such as Image
- XmlList maps a property to a list
- XmlMimeType identifies a textual representation of the mime type for a property
- XmlMixed identifies a multivalued property with mixed content
- XmlNs identifies an XML namespace
- XmlRegistry marks a class that has @XmlElementDecl
- XmlRootElement maps a class or enumeration to an XML element
- XmlSchema identifies a target namespace for a package
- XmlSchemaType maps a Java type to a built-in schema type
- XmlSchemaTypes acts as a container for multiple @XmlSchemaType annotations
- XmlTransient sets a property that shouldn"t be saved
- XmlType maps a class or enumeration to a schema type
- XmlValue maps of a class to a simple schema content or type