JAXBのXsd to Java Object.

Xsd to java Object.

right, use xsd to definition the xml 's struction, then generator the Java construction.

right, when you generator the Java construction, then fill the Object some data, then can generator the xml what the xsd defnied.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
            jxb:version="2.0">

<xsd:element name="Greetings" type="GreetingListType"/>

<xsd:complexType name="GreetingListType">
  <xsd:sequence>
    <xsd:element name="Greeting" type="GreetingType"
                 maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="GreetingType">
  <xsd:sequence>
    <xsd:element name="Text" type="xsd:string"/>
  </xsd:sequence>
  <xsd:attribute name="language" type="xsd:language"/>
</xsd:complexType>

</xsd:schema>



xjc -p ycl.learn.xml.hello hello.xsd


then will be generator files below.
//
// 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: 2013.12.09 at 12:51:43 PM CST 
//


package ycl.learn.xml.hello;

import java.util.ArrayList;
import java.util.List;
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 GreetingListType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="GreetingListType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Greeting" type="{}GreetingType" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GreetingListType", propOrder = {
    "greeting"
})
public class GreetingListType {

    @XmlElement(name = "Greeting", required = true)
    protected List<GreetingType> greeting;

    /**
     * Gets the value of the greeting property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the greeting property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getGreeting().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link GreetingType }
     * 
     * 
     */
    public List<GreetingType> getGreeting() {
        if (greeting == null) {
            greeting = new ArrayList<GreetingType>();
        }
        return this.greeting;
    }

}


this clazz will be point which section of xsd it generator.
@XmlType(name = "GreetingListType", propOrder = {
    "greeting"
})
definition this class as GreetingListType.

@XmlElement(name = "Greeting", required = true)
    protected List<GreetingType> greeting;
definition this properties as greeting.

//
// 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: 2013.12.09 at 12:51:43 PM CST 
//


package ycl.learn.xml.hello;

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.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


/**
 * <p>Java class for GreetingType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="GreetingType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Text" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *       &lt;attribute name="language" type="{http://www.w3.org/2001/XMLSchema}language" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GreetingType", propOrder = {
    "text"
})
public class GreetingType {

    @XmlElement(name = "Text", required = true)
    protected String text;
    @XmlAttribute
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "language")
    protected String language;

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

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

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

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

}



@XmlType(name = "GreetingType", propOrder = {
    "text"
})
this is the greetingType class.
and there definied some xmlelement, xmlattribute.
    @XmlAttribute
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "language")
this is more complex annotation.

//
// 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: 2013.12.09 at 12:51:43 PM CST 
//


package ycl.learn.xml.hello;

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 ycl.learn.xml.hello 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 _Greetings_QNAME = new QName("", "Greetings");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ycl.learn.xml.hello
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link GreetingType }
     * 
     */
    public GreetingType createGreetingType() {
        return new GreetingType();
    }

    /**
     * Create an instance of {@link GreetingListType }
     * 
     */
    public GreetingListType createGreetingListType() {
        return new GreetingListType();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link GreetingListType }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "", name = "Greetings")
    public JAXBElement<GreetingListType> createGreetings(GreetingListType value) {
        return new JAXBElement<GreetingListType>(_Greetings_QNAME, GreetingListType.class, null, value);
    }

}



this is the Object factory.
so how we can use this generators.

package ycl.learn.xml.hello;

import java.util.*;
import javax.xml.bind.*; 

public class Hello {

    private ObjectFactory of;
    private GreetingListType grList;

    public Hello(){
        of = new ObjectFactory();
        grList = of.createGreetingListType();
    }

    public void make( String t, String l ){
        GreetingType g = of.createGreetingType();
        g.setText( t );
        g.setLanguage( l );
        grList.getGreeting().add( g );
    }

    public void marshal() {
        try {
            JAXBElement<GreetingListType> gl =
                of.createGreetings( grList );
            JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class);
            Marshaller m = jc.createMarshaller();
            m.marshal( gl, System.out );
        } catch( JAXBException jbe ){
            // ...
        	jbe.printStackTrace();
        }
    }
    
    public static void main(String args[]){
    	Hello h = new Hello();
    	h.make( "Bonjour, madame", "fr" ); 
    	h.make( "Hey, you", "en" ); 
    	h.marshal();
    }
}



and use this, we will generator as below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Greetings>
	<Greeting language="fr">
		<Text>Bonjour, madame</Text>
	</Greeting>
	<Greeting language="en">
		<Text>Hey, you</Text>
	</Greeting>
</Greetings>


walk back for one step, If we don't generator xsd, we just generator owner Java object to unmarshal this file.
package ycl.learn.xml.hello;

import java.util.List;

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

@XmlRootElement(name="Greetings")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Greetings", propOrder = {
    "greetings"
})
public class Greetings {
	
	@XmlElement(name="Greeting")
	private List<Greeting> greetings;

	public List<Greeting> getGreetings() {
		return greetings;
	}

	public void setGreetings(List<Greeting> greetings) {
		this.greetings = greetings;
	}
	
	
}



package ycl.learn.xml.hello;

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.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Greeting", propOrder = {
		"text"
	})
public class Greeting {

	 	@XmlElement(name = "Text", required = true)
	    private String text;
	    @XmlAttribute
	    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
	    @XmlSchemaType(name = "language")
	    private String language;
	    
		public String getLanguage() {
			return language;
		}
		public void setLanguage(String language) {
			this.language = language;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
	    
	    
}



package ycl.learn.xml.hello;

import java.io.File;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBException;

import ycl.learn.xml.jaxb.JAXBUtil;

public class TestGreetings {

	private static final String FILE_NAME_emps = "greetings.xml";
	/**
	 * @param args
	 * @throws JAXBException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, JAXBException { 
		JAXBUtil<Greetings> ju = new JAXBUtil<Greetings>();
		Greetings gs = ju.unmarshal(Greetings.class, new File(FILE_NAME_emps));
		for(Greeting g:gs.getGreetings()){
			System.out.println(g.getText());
			System.out.println(g.getLanguage());
		}
	}

}


this is work well, and the code is formated.
I think if you can definied this class construction by yourself, don't lazy to do it.
It will be effect to your future work.
And i think use xsd to generator Java construction is not a good idea, we can definied by our thinking.
but whatever, we can generator it, then look look the system's function.

你可能感兴趣的:(object)