`

JAXBのXsd to Java Object.

    博客分类:
  • JAXB
 
阅读更多
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.
分享到:
评论

相关推荐

    如何使用JAXB框架定制Web服务行为.doc

    JAXB(Java Architecture for XML Binding)是Java平台上用于XML Schema和Java对象之间绑定的标准框架,它简化了XML数据与Java应用程序之间的交互。JAXB允许开发者将XML文档转换为Java对象,反之亦然,使得处理XML...

    JAXB工具类 xml转为java对象 java对象转为xml

    JAXB提供了一种自动化的机制,将XML Schema(XSD)映射到Java类,反之亦然。这允许开发者在Java应用中存储和序列化数据为XML格式,同时保持代码的简洁性和可维护性。JAXB的核心组件包括: 1. **Unmarshaller**: ...

    java生成xsd,xml示例

    在Java中生成XML文档,我们可以使用DOM(Document Object Model)、SAX(Simple API for XML)或者JAXB(Java Architecture for XML Binding)。DOM解析器会将整个XML文档加载到内存中,适合小规模数据处理;SAX是...

    webservice之使用jaxb把xml转换Object或把对象转换成xml文件

    2. 生成Java类:使用JAXB工具(如`xjc`命令行工具或Maven插件)对XSD文件进行编译,自动生成对应的Java类。这些类包含了XML元素和属性的映射。 3. 解析XML:有了Java类,我们就可以使用JAXB提供的`Unmarshaller`...

    jaxb (XML操作)

    1. **绑定(Binding)**:JAXB通过绑定文件(一般为.xjb或绑定注解)将XML Schema(XSD)或者Java类与Java类结构关联起来。这个过程创建了一个Java类模型,其中每个XML元素和属性都对应Java类的字段或方法。 2. **...

    XML How to Program-Java.zip_XML java_java how to program_java xm

    "XML How to Program - Java.zip"可能是一个包含关于如何使用Java处理XML的教程或参考材料。 Java API for XML Processing (JAXP) 是Java平台的标准部分,它提供了处理XML的基本工具。JAXP允许开发者进行XML解析、...

    xml_programming_in_java.rar_XML java_java programming_xml how to

    - DOM(Document Object Model):Java中的DOM解析器如`javax.xml.parsers.DocumentBuilderFactory`,允许开发者将整个XML文档加载到内存中,形成一个树形结构,方便遍历和操作。 - SAX(Simple API for XML):...

    XML Programming in Java.rar_java programming_java xml_xml

    3. **JAXB(Java Architecture for XML Binding)**:JAXB是Java标准,用于在Java对象和XML文档之间进行自动转换。通过注解或XML Schema,我们可以将Java类映射到XML元素,实现序列化和反序列化。 4. **StAX...

    JAXB Unmarshalling @XmlAnyElement

    JAXB(Java Architecture for XML Binding)是一种将Java对象与XML文档绑定的技术。它可以用于将Java对象序列化为XML格式,也可以将XML文档反序列化为Java对象。在处理XML文档时,经常会出现一个问题:如何将一个...

    jaxb xml转化为对象,对象转化为xml,xml转化为xsd,表转化为对象

    JAXB并不直接支持数据库操作,但它可以与ORM(Object-Relational Mapping)框架如JPA(Java Persistence API)或Hibernate配合使用。这些框架可以将数据库表映射为Java实体类,而JAXB则负责将这些实体类转化为XML...

    Java&XML.rar_XML java_xml

    在Java中,可以用JAXB或JAXP验证XML文档是否符合指定的XSD。 8. **Spring框架**:在Java企业级开发中,Spring框架广泛使用XML配置文件来管理bean的依赖注入,提供服务定位和配置。 这个项目适合初学者,因为它提供...

    Java XML编程指南

    Java提供了一整套API来处理XML,包括SAX(Simple API for XML)、DOM(Document Object Model)和JAXB(Java Architecture for XML Binding)等。 1. SAX:SAX是一个事件驱动的解析器,适用于处理大型XML文档。它...

    Xsd转换xml

    例如,Java中可以使用JAXB(Java Architecture for XML Binding)或者XSOM(XML Schema Object Model)库,这些库提供了从XSD生成XML的能力。C#中,.NET Framework提供了System.Xml.Schema命名空间,其中的类可以...

    xml文件自动转成java对象工具

    5. **生成序列化和反序列化代码**:为了方便XML数据和Java对象之间的转换,工具还会生成解析XML和生成XML的代码,通常是基于JAXB(Java Architecture for XML Binding)或其他类似库。 除了XSD转Java,有些工具还...

    Java与XML数据绑定

    首先,Java中XML数据绑定的主要API有JAXB(Java Architecture for XML Binding)和DOM(Document Object Model)。JAXB允许开发者在Java类和XML之间进行自动转换,而DOM则是XML文档的一种树形结构表示,用于在内存中...

    XML JAVA指南

    Java提供了丰富的API来处理XML,如DOM、SAX、StAX和JAXB等解析器。本指南将深入探讨Java与XML的交互。 1. **DOM解析器**:Document Object Model(DOM)是一种将整个XML文档加载到内存中以形成一棵树形结构的方法。...

    JAVA对象序列化保存为XML文件的工具类

    在这些情况下,可能需要使用更强大的库,如JAXB(Java Architecture for XML Binding)或DOM(Document Object Model)API来实现更精细的控制。然而,对于简单的对象序列化需求,`XMLEncoder`和`XMLDecoder`是相当...

    java读写xml

    JAXBContext context = JAXBContext.newInstance(MyObject.class); MyObject obj = new MyObject(); //...设置对象属性 Marshaller marshaller = context.createMarshaller(); marshaller.marshal(obj, System.out);...

    spring-oxm-4.2.xsd.zip

    它提供了一种抽象层,使开发者能够以统一的方式处理多种不同的XML绑定技术,如JAXB、Castor、XML Beans等。Spring OXM通过XML Schema定义了配置文件的规则,其中`spring-oxm-4.2.xsd`就是这个版本的OXM模块的XML ...

    java+xml联合编程简单例子

    3. **JAXB(Java Architecture for XML Binding)**:JAXB允许我们将Java对象和XML文档之间进行自动转换。Java类可以被编译为XML Schema,反之亦然,这简化了数据的序列化和反序列化过程。 4. **JDOM(Java ...

Global site tag (gtag.js) - Google Analytics