| Refresh | Home EGTry.com

jaxb map schema to java class


step 1. create a xml schema

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="hello" type="helloType"/>
 <xsd:complexType name="helloType">
  <xsd:attribute name="msg" type="xsd:string"/>
 </xsd:complexType>
</xsd:schema>


step 2. generate java sources from the xml schema
 xjc -p lab.xml.jaxb lab\xml\jaxb\hello.xsd

generate the following java sources files

lab\xml\jaxb\HelloType.java
lab\xml\jaxb\ObjectFactory.java


step 3. add XmlRootElement annotation to HelloType.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.3 in JDK 1.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.03.24 at 11:43:08 PM CDT 
//


package lab.xml.jaxb;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for helloType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="helloType">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <attribute name="msg" type="{http://www.w3.org/2001/XMLSchema}string" />
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@XmlRootElement(name="hello")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "helloType")
public class HelloType {

    @XmlAttribute
    protected String msg;

    /**
     * Gets the value of the msg property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getMsg() {
        return msg;
    }

    /**
     * Sets the value of the msg property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setMsg(String value) {
        this.msg = value;
    }

}


step 4. use the code. create the java object and marshall into xml

package lab.xml.jaxb;


import java.io.PrintWriter;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;


public class HelloMain {

	/**
	 * @param args
	 */
	public static void main(String[] args)  throws Exception{
		PrintWriter out=new PrintWriter(System.out);
		test(out);
		out.flush();
	}
	
	public static void test(PrintWriter out) throws Exception {
		JAXBContext JContext=JAXBContext.newInstance("lab.xml.jaxb");
		
		//marshalling, from object to xml
		ObjectFactory oFactory=new ObjectFactory();
		HelloType helloT=oFactory.createHelloType();
		helloT.setMsg("hello jaxb");
		Marshaller m=JContext.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		StringWriter writer=new StringWriter();
		m.marshal(helloT, writer);
		String outputXml=writer.toString();
		out.println("xml output:\n"+outputXml);
		

	}

}