`

JAXB入门

 
阅读更多
引用
jaxb是一个读写xml的工具,还可以提供验证,不需要额外的jar


1. XSD sample
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org/draft_2/"
	targetNamespace="http://www.example.org/draft_2/">

	<xs:complexType name="BaseCommonRefCType">
		<xs:sequence>
			<xs:element name="name">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:minLength value="1" />
						<xs:maxLength value="64" />
					</xs:restriction>
				</xs:simpleType>
			</xs:element>
			<xs:element name="type" maxOccurs="1"
						minOccurs="0">
				<xs:simpleType>
					<xs:restriction base="xs:string">
						<xs:minLength value="1" />
						<xs:maxLength value="64" />
						<xs:enumeration value="filter" />
						<xs:enumeration value="nzload" />
						<xs:enumeration value="db_system" />
											<xs:enumeration value="repetition_group" />
						<xs:enumeration value="system_path" />
						<xs:enumeration value="record" />
						<xs:enumeration value="report" />
						<xs:enumeration value="mapping" />
						<xs:enumeration value="table" />
						<xs:enumeration value="cob" />
						<xs:enumeration value="file_container" />
						<xs:enumeration value="node" />
						<xs:enumeration value="instance" />
						<xs:enumeration value="stage" />
						<xs:enumeration value="java_config" />
						<xs:enumeration value="business_date" />
						<xs:enumeration value="matrix" />
						<xs:enumeration value="database" />
						<xs:enumeration value="file_pattern" />
						<xs:enumeration value="gemfire_region" />
						<xs:enumeration value="task" />
						<xs:enumeration value="config_file" />
						<xs:enumeration value="dataflow" />
						<xs:enumeration value="ems" />
						<xs:enumeration value="workflow" />
					</xs:restriction>
				</xs:simpleType>
			</xs:element>

		</xs:sequence>
	</xs:complexType>
</xs:schema>


2. 根据xsd生成java类
xjc -p com.wilson test.xsd -d   src



3. Sample class for retrieving classes from xml
public class JAXBUtil {
	private static final Log logger = LogFactory.getLog(JAXBUtil.class.getName());

	public static YourClass retrieveObjectFromXML(File xml, URL xsdURL) {
		ProcessLifeCycle lifeCycle = null;
		JAXBContext jaxbContext;
		ValidationEventCollector vec = new ValidationEventCollector();

		try {
			jaxbContext = JAXBContext.newInstance(YourClass.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
			
			

			
			unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsdURL));
			unmarshaller.setEventHandler(vec);
			lifeCycle = (ProcessLifeCycle) unmarshaller.unmarshal(xml);

		} catch (JAXBException e) {
			logger.error("", e);
		} catch (SAXException e) {
			logger.error("", e);
		} finally {
			if (vec != null && vec.hasEvents()) {
				for (ValidationEvent ve : vec.getEvents()) {
					String msg = ve.getMessage();
					ValidationEventLocator vel = ve.getLocator();
					int line = vel.getLineNumber();
					int column = vel.getColumnNumber();
					throw new RuntimeException("Can't unmarshal the XML file, error message: " + " At line " + line + ", column " + column + ": "
							+ msg);
				}
			}
		}
		return lifeCycle;
	}
}
分享到:
评论

相关推荐

    XML JAXB入门示例

    很好的jaxb入门示例 把所有注解标签都试一遍基本就OK了

    JAXB入门学习(五)

    NULL 博文链接:https://popomonkey.iteye.com/blog/688580

    jaxb入门学习

    这是一个不错的文档哦,希望能下下来参考看看

    JAXB实例入门

    NULL 博文链接:https://rensanning.iteye.com/blog/2378681

    JAXB资料集.rar

    资料比较完整:Jaxb2_技术指南,JAXB的使用入门,JAXB介绍.ppt,JAXBDemos

    JiBx开发入门指南

    JiBx又一个不错的xml绑定工具,和jaxb等一样,都是属于xml绑定工具。而JiBx使用java字节码enhance技术,不像jaxb等更多在于源代码生成技术。JiB的工作主要在于前期,也就是进行字节码绑定,这一部分基本上都是在...

    WebService CXF学习文档

    WebService CXF学习——入门篇 1.CXF由来 2.HelloWorld 3.WSDL描述 WebService CXF学习——进阶篇 1.SOAP讲解 2.JAX-WS讲解 3.对象传递 WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF...

    JSON-TF:这是JSON Task Force的游乐场。 寻求使JSON成为PESC和A4L规范中的一流公民

    入门安装节点和NPM 和才能运行测试。运行测试$ npm test示例输出$ npm test&gt; json-tf@1.0.0 test /Users/sallen/Projects/Demo/JSON-TF&gt; mocha xStudentsGoessner.json #familyName ✓ should find "Lovell" as ...

    Java SE实践教程 pdf格式电子书 下载(四) 更新

    9.2.2 我的联系手册(JAXB实现) 238 9.3 小结 242 第10章 准备环球旅行——应用程序国际化 243 10.1 讲解 244 10.1.1 概念介绍 244 10.1.2 设置Locale 249 10.1.3 隔离语言环境相关数据 252 10.1.4 格式化 ...

    Spring.3.x企业应用开发实战(完整版).part2

    14.3.1 JAXB 14.3.2 XMLBeans 14.3.3 Castor 14.3.4 JiBX 14.3.5 总结比较 14.4 与Spring OXM整合 14.4.1 Spring OXM概述 14.4.2 整合OXM实现者 14.4.3 如何在Spring中进行配置 14.4.4 Spring OXM 简单实例 14.5 小...

    Spring3.x企业应用开发实战(完整版) part1

    14.3.1 JAXB 14.3.2 XMLBeans 14.3.3 Castor 14.3.4 JiBX 14.3.5 总结比较 14.4 与Spring OXM整合 14.4.1 Spring OXM概述 14.4.2 整合OXM实现者 14.4.3 如何在Spring中进行配置 14.4.4 Spring OXM 简单实例 14.5 小...

Global site tag (gtag.js) - Google Analytics