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>



相关文章
|
IDE 开发工具
Unable to load class ‘org.gradle.api.internal.plugins.DefaultConvention‘
Unable to load class ‘org.gradle.api.internal.plugins.DefaultConvention‘
1155 0
Unable to load class 'org.gradle.api.internal.component.Usage
Unable to load class 'org.gradle.api.internal.component.Usage
88 0
|
API
The bean ‘api‘, defined in class path resource [com/common/swagger/SwaggerAutoConf
The bean ‘api‘, defined in class path resource [com/common/swagger/SwaggerAutoConf
100 0
The bean ‘api‘, defined in class path resource [com/common/swagger/SwaggerAutoConf
|
应用服务中间件
Cannot change version of project facet Dynamic Web Module to 3.0
Cannot change version of project facet Dynamic Web Module to 3.0
|
Java
全网首发:org.xml.sax.SAXNotRecognizedException: unrecognized features nonvalidating/load-external-dtd
全网首发:org.xml.sax.SAXNotRecognizedException: unrecognized features nonvalidating/load-external-dtd
574 0
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
138 0
Faceted project metadata file "/.settings/org.eclipse.wst.common.project.facet.core.xml" could
|
C++
C++ runtime sample
本文演示如何利用函数计算的自定义Runtime功能来运行C++代码
1737 0

热门文章

最新文章