especialjie 2013-08-11
@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public class Customer {
@XmlAttribute(required = true)
protected String name;
@XmlAttribute(required = true)
protected int age;
/**
* 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 age property.
*
*/
public int getAge() {
return age;
}
/**
* Sets the value of the age property.
*
*/
public void setAge(int value) {
this.age = value;
}
}
public class SoapClient {
private final static String MODEL = "com.itdcl.model";
public static void main(String[] args) throws ParserConfigurationException, JAXBException, TransformerException{
ObjectFactory factory = new ObjectFactory();
Customer customer = factory.createCustomer();
customer.setAge(20);
customer.setName("Josen");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
//Java对象转换成XML
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, doc);
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String xmlString = writer.toString();
System.out.println(xmlString);
//XML转换成Java对象
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xmlString);
Customer cus = (Customer)unmarshaller.unmarshal(reader);
System.out.println("Age:"+cus.getAge());
System.out.println("Name:"+cus.getName());
}
} JAXBContext jaxbContext = JAXBContext.newInstance(MODEL); //Java对象转换成XML Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(customer, doc); DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); String xmlString = writer.toString(); System.out.println(xmlString);
JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xmlString);
Customer cus = (Customer)unmarshaller.unmarshal(reader);
System.out.println("Age:"+cus.getAge());
System.out.println("Name:"+cus.getName()); <?xml version="1.0" encoding="utf-8" ?>
<project default="xjc-compile" basedir=".">
<property name="src.dir" location="src" />
<property name="lib.dir" location="E:/cxf-lib" />
<property name="xml-schema.dir" location="src/WEB-INF" />
<property name="schema.name" value="cxfdemo.xsd" />
<property name="package" value="com.itdcl.model" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"
classpathref="classpath" />
<target name="xjc-compile">
<echo message="Build Jaxb Class from Schema" />
<xjc schema="${xml-schema.dir}/${schema.name}"
destdir="${src.dir}" package="${package}" >
<produces dir="src/com/itdcl/model" includes="*" />
</xjc>
</target>
</project>