/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//将XSD反编组成POJO后,相应的类存放的包路径就是下面
//JAXB的JAXBContext上下文。
JAXBContext jc = JAXBContext.newInstance("demo.jaxb.basic");
Unmarshaller un = jc.createUnmarshaller();
User user = (User) un.unmarshal(new File("user.xml"));
System.out.println(user.getUserAge());
System.out.println(user.getUserID());
System.out.println(user.getUserName());
user.setUserAge(100);
user.setUserID("eric");
user.setUserName("ericwang");
Marshaller ma = jc.createMarshaller();
ma.marshal(user, new File("enshiwzw.xml"));
//use the ObjectFactory to create an object and xml
ObjectFactory of = new ObjectFactory();
User user1 = of.createUser();
user1.setUserAge(26);
user1.setUserID("nma");
user1.setUserName("张某人");
String name = user1.getUserName();
byte b[]=name.getBytes();
// name = new String(b,"");
user.setUserName(name);
System.out.println(name);
ma.marshal(user1, new File("enshiwzw1.xml"));
ma.marshal(user1, System.out);
//
}
生成的代码如下:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-646
// 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: 2008.09.22 at 03:37:42 下午 CST
//
package demo.jaxb.basic;
import javax.xml.bind.annotation.XmlRegistry;
/**
* 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 {
/**
* 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 User }
*
*/
public User createUser() {
return new User();
}
}
如下:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-646
// 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: 2008.09.22 at 03:37:42 下午 CST
//
/**
* Gets the value of the userID property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUserID() {
return userID;
}
/**
* Sets the value of the userID property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUserID(String value) {
this.userID = value;
}
/**
* Gets the value of the userName property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUserName() {
return userName;
}
/**
* Sets the value of the userName property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUserName(String value) {
this.userName = value;
}
/**
* Gets the value of the userAge property.
*
*/
public int getUserAge() {
return userAge;
}
/**
* Sets the value of the userAge property.
*
*/
public void setUserAge(int value) {
this.userAge = value;
}