JAXB Sample

简介: <div> <div> <div>1. Schema文件:</div> <div><pre code_snippet_id="652914" snippet_file_name="blog_20150425_1_726077" name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><sche
1. Schema文件:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.liulutu.com/students/" targetNamespace="http://www.liulutu.com/students/">
	<element name="students">
		<complexType>
			<sequence>
				<element name="student" type="tns:StudentType" maxOccurs="unbounded" />
			</sequence>
		</complexType>
	</element>

	<simpleType name="SexType">
		<restriction base="string">
			<enumeration value="Male" />
			<enumeration value="Female" />
		</restriction>
	</simpleType>

	<complexType name="StudentType">
		<attribute name="sex" type="tns:SexType" />
		<attribute name="name" type="string" />
	</complexType>
</schema>

2. 生成Model类:
  • Eclipse开发环境中,右键选中Schema文件 --> Generate --> JAXB Classes


  • 生成的JAXB Model Classes 如下:


3. 测试启动类:
package demo;

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

import demo.model.ObjectFactory;
import demo.model.SexType;
import demo.model.StudentType;
import demo.model.Students;

public class JaxbDemo {

	private static void addStudent(ObjectFactory factory, Students students, String name, SexType type) {
		StudentType studentType = factory.createStudentType();

		studentType.setName(name);
		studentType.setSex(type);

		students.getStudent().add(studentType);
	}

	public static void main(String[] args) throws JAXBException {
		ObjectFactory factory = new ObjectFactory();
		Students students = factory.createStudents();

		JaxbDemo.addStudent(factory, students, "Jim", SexType.MALE);
		JaxbDemo.addStudent(factory, students, "Merry", SexType.FEMALE);

		JAXBContext jaxbContext = JAXBContext.newInstance("demo.model");

		Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		marshaller.marshal(students, System.out);
	}

}

4. 结果输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:students xmlns:ns2="http://www.liulutu.com/students/">
    <student sex="Male" name="Jim"/>
    <student sex="Female" name="Merry"/>
</ns2:students>



相关文章
|
9月前
|
Dubbo Java 应用服务中间件
Serialized class org.apache.catalina.core.ApplicationPart must implement java.io.Serializable
Serialized class org.apache.catalina.core.ApplicationPart must implement java.io.Serializable
122 0
|
6月前
|
Java Maven Windows
java -jar 启动 boot 程序 no main manifest attribute, in .\vipsoft-model-web-0.0.1-SNAPSHOT.jar
java -jar 启动 boot 程序 no main manifest attribute, in .\vipsoft-model-web-0.0.1-SNAPSHOT.jar
85 0
|
分布式计算 Hadoop
SLF4J:Failed to load class org.slf4j.impl.StaticLoggerBinder.
SLF4J:Failed to load class org.slf4j.impl.StaticLoggerBinder.
281 0
|
Java 数据库连接
org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration
Error creating bean with name 'entityManagerFactory' defined in file [E:\eclipseworkspace\wms_ims\.metadata\.plugins\org.eclipse.wst.server.core\tmp9\wtpwebapps\shopping\WEB-INF\classes\applicationContext-configuration.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMetho
111 0
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.
141 0
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.
|
Java Android开发
Unable to load class ‘javax.xml.bind.annotation.XmlSchema‘.
Unable to load class ‘javax.xml.bind.annotation.XmlSchema‘.
925 0
Unable to load class ‘javax.xml.bind.annotation.XmlSchema‘.
Faceted project metadata file "/.settings/org.eclipse.wst.common.project.facet.core.xml" could
Faceted project metadata file "/.settings/org.eclipse.wst.common.project.facet.core.xml" could
184 0
Faceted project metadata file "/.settings/org.eclipse.wst.common.project.facet.core.xml" could
|
Java
全网首发:org.xml.sax.SAXNotRecognizedException: unrecognized features nonvalidating/load-external-dtd
全网首发:org.xml.sax.SAXNotRecognizedException: unrecognized features nonvalidating/load-external-dtd
703 0