[b]a.xml[/b]
[b]a.xsd[/b]
[b]xjc生成java类[/b]
D:\JavaSoft\jdk6\bin>xjc -d f:/xmldemo -p demo.xml f:/xmldemo/a.xsd
parsing a schema...
compiling a schema...
demo\xml\Address.java
demo\xml\Customer.java
demo\xml\ObjectFactory.java
[b]Customer.java[/b]
[b]Address.java[/b]
[b]ObjectFactory.java[/b]
[b]JaxbDemo.java[/b]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="cust id">
<address>
<street>jinjihulu</street>
<city>suzhou</city>
<zip>215200</zip>
</address>
<name>Marshall</name>
</customer>
[b]a.xsd[/b]
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Customer">
<xs:sequence>
<xs:element name="address" type="Address"/>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
<xs:complexType name="Address">
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="zip" type="ZipCodeType"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="ZipCodeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="10000"/>
<xs:maxInclusive value="99999"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="customer" type="Customer"/>
<xs:element name="address" type="Address"/>
</xs:schema>
[b]xjc生成java类[/b]
D:\JavaSoft\jdk6\bin>xjc -d f:/xmldemo -p demo.xml f:/xmldemo/a.xsd
parsing a schema...
compiling a schema...
demo\xml\Address.java
demo\xml\Customer.java
demo\xml\ObjectFactory.java
[b]Customer.java[/b]
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2011.12.30 at 04:13:13 PM CST
//
package demo.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for Customer complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="Customer">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="address" type="{}Address"/>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* <attribute name="id" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Customer", propOrder = {
"address",
"name"
})
public class Customer {
@XmlElement(required = true)
protected Address address;
@XmlElement(required = true)
protected String name;
@XmlAttribute
protected String id;
/**
* Gets the value of the address property.
*
* @return
* possible object is
* {@link Address }
*
*/
public Address getAddress() {
return address;
}
/**
* Sets the value of the address property.
*
* @param value
* allowed object is
* {@link Address }
*
*/
public void setAddress(Address value) {
this.address = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the id property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getId() {
return id;
}
/**
* Sets the value of the id property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setId(String value) {
this.id = value;
}
}
[b]Address.java[/b]
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2011.12.30 at 04:13:13 PM CST
//
package demo.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for Address complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="Address">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="zip" type="{}ZipCodeType"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Address", propOrder = {
"street",
"city",
"zip"
})
public class Address {
@XmlElement(required = true)
protected String street;
@XmlElement(required = true)
protected String city;
protected int zip;
/**
* Gets the value of the street property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getStreet() {
return street;
}
/**
* Sets the value of the street property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setStreet(String value) {
this.street = value;
}
/**
* Gets the value of the city property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getCity() {
return city;
}
/**
* Sets the value of the city property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCity(String value) {
this.city = value;
}
/**
* Gets the value of the zip property.
*
*/
public int getZip() {
return zip;
}
/**
* Sets the value of the zip property.
*
*/
public void setZip(int value) {
this.zip = value;
}
}
[b]ObjectFactory.java[/b]
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2011.12.30 at 04:13:13 PM CST
//
package demo.xml;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the generated package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _Address_QNAME = new QName("", "address");
private final static QName _Customer_QNAME = new QName("", "customer");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Customer }
*
*/
public Customer createCustomer() {
return new Customer();
}
/**
* Create an instance of {@link Address }
*
*/
public Address createAddress() {
return new Address();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Address }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "address")
public JAXBElement<Address> createAddress(Address value) {
return new JAXBElement<Address>(_Address_QNAME, Address.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Customer }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "customer")
public JAXBElement<Customer> createCustomer(Customer value) {
return new JAXBElement<Customer>(_Customer_QNAME, Customer.class, null, value);
}
}
[b]JaxbDemo.java[/b]
package demo.xml;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class JaxbDemo {
public static void main(String[] args) throws Exception {
unmarshalling();
marshalling();
}
static void unmarshalling() throws Exception{
JAXBContext jc = JAXBContext.newInstance("demo.xml");
Unmarshaller u = jc.createUnmarshaller();
JAXBElement customerE = (JAXBElement) u.unmarshal(new FileInputStream(
"F:\\xmldemo\\a.xml"));
Customer bo = (Customer) customerE.getValue();
System.out.println(bo.getName() + " : " + bo.getAddress().getCity() + " : " + bo.getId());
}
static void marshalling()throws Exception{
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller ms = jc.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Customer cust = new Customer();
cust.setId("cust id");
cust.setName("Marshall");
Address a = new Address();
a.setCity("suzhou");
a.setStreet("jinjihulu");
a.setZip(215200);
cust.setAddress(a);
OutputStream os = new FileOutputStream("F:\\xmldemo\\a.xml");
ms.marshal(cust, os);
os.close();
}
}