JAXB - Hello World
We'll stick with the tradition and use a sort of "Hello World" XML document to illustrate the typical scenario for creating the Java classes and their use to marshal a document. We'll not discuss any details in this subsection; it's just here to give you the overall picture.
The XML Schema on hello.xsd defines the structure of our document, which is to contain a series of salutations, each of which contains a greeting (such as "Hello world") and an attribute for registering the language of the salutation.
<?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>
Now we can call the JAXB schema compiler, defining the package name hello for the generated classes.
xjc -p hello hello.xsd
This generates several classes in the subdirectory hello. The class Hello shows how to use them.
import java.util.*;
import javax.xml.bind.*;
import hello.*; 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( "hello" );
Marshaller m = jc.createMarshaller();
m.marshal( gl, System.out );
} catch( JAXBException jbe ){
// ...
}
}
}
The constructor uses a method from the object factory to create an object of the document's top level XML element type, i.e., GreetingListType. The make method adds another salutation with its text element and the language attribute. Finally, with a call to marshal, the list is wrapped in its XML element, and the resulting XML document is written to the standard output stream. Here's a sequence of these calls:
Hello h = new Hello();
h.make( "Bonjour, madame", "fr" );
h.make( "Hey, you", "en" );
h.marshal();
The output is shown below, formatted, for better readability.
<?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>
JAXB - Hello World的更多相关文章
- XmlRootElement JAXB
		http://desert3.iteye.com/blog/1570092(文章已经很好) 看了那边文章以后尝试后写点直白的 PROPERTY: JAXB 绑定类中的每个获取方法/设置方法对将会自动绑 ... 
- 错误:java.util.Map is an interface, and JAXB can't handle interfaces.
		问题: 在整合spring+cxf时报错java.util.Map is an interface, and JAXB can't handle interfaces. 解决方法: 将服务端的serv ... 
- jaxb
		一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ... 
- Jaxb annotation使用
		JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ... 
- JAXB最佳实践
		JAXB主要用来实现对象和XML之间的序列化和反序列化. 本文主要总结JAXB基本使用方法和注意事项! 通过下文的XML示例内容进行JAXB的简单实践 <?xml version="1 ... 
- 在Gradle中使用jaxb的xjc插件
		jaxb,全称为Java Architecture for Xml Binding,是一种将java对象与xml建立起映射的技术.其主要提供两个功能,一是将java对象映射为xml,二是将xml映射为 ... 
- Java for XML: JAXP、JAXB、JAXM、JAX-RPC、JAX-WS
		在XML领域里,对XML文件的校验有两种方式:DTD校验.Schema校验.在Java中,对于XML的解析,有多种方式:DOM解析.SAX解析.StAX解析.结合XML和Java后,就产生了Bind技 ... 
- XStream、JAXB 日期(Date)、数字(Number)格式化输出xml
		XStream.Jaxb是java中用于对象xml序列化/反序列化 的经典开源项目,利用它们将对象转换成xml时,经常会遇到日期(Date).数字按指定格式输出的需求,下面是使用示例: 一.日期字段格 ... 
- java JAXB 学习
		JAXB(Java Architecture for XML Binding)是JDK的一部分,用于Object <-> XML的转换(有点类似于.NET中的XML序列化). 1.创建XS ... 
- Jaxb 解析 带有继承关系的bean与xml
		具体方法: 1. 在jaxb的setClasstobebounds中,只需要子类的class,无需父类. 2. 父类的前面加如下声明: @XmlAccessorType(XmlAccessType.F ... 
随机推荐
- 如何自己动手实现 KVO(转)
			KVO (Key-Value Observing) KVO 是 Objective-C 对观察者模式(Observer Pattern)的实现.也是 Cocoa Binding 的基础.当被观察对象的 ... 
- emWin -- 模拟器系列1 - 如何建立模拟器开发环境
			面对如此强大的emWin,大家是否都有跃跃欲试的冲动呢?但是没有硬件可以调试的童鞋,难道只能望洋兴叹?非也.非也.Segger公司早就考虑到了.Segger推出模拟器的目的不仅仅是为了解决没有硬件的烦 ... 
- web.xml(spring/spring mvc/hibernate)
			<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" ... 
- js主要知识轮廓笔记
			一.js中的基础类型和引用类型: 基础类型:1.Number2.String3.Boolean4.Undefined5.Null 引用类型(内置对象):1.Object类型2.Array类型3.Dat ... 
- Java的位运算符实例——与(&)、非(~)、或(|)、异或(^)
			一.Java的位运算符实例——与(&).非(~).或(|).异或(^) 1.与(&) 0 & 2 = 0 0 0 0 0 1 0 0 1 0 2.非(~) ~0 = 7 0 0 ... 
- c/c++中使用指针需要注意的问题
			一.使用指针的时候需要注意几点: 分配空间 初始化 释放 二.常见的错误有几种: 1)内存分配未成功,却使用了它 编程新手常犯这种错误,因为他们没有意识到内存分配会不成功.常用解决办法是,使 ... 
- iOS调用系统通讯录获取姓名电话号码(转)
			原文地址:http://blog.csdn.net/idoshi201109/article/details/46007125 OS调用系统通讯录获取姓名电话号码 (iOS 8.0 Xcode6.3可 ... 
- UITableView实现格瓦拉飞天投票模块
			格瓦拉目前来说动画效果确实做的还比较好,虽然不是说很炫但做到精致,这次就模仿了它投票的模块.其实想到要实现它还是有很多方法,不过这次我还是采用了苹果自带控件UITableView简简单单来实现它,再次 ... 
- mysql 源码--xpchild
			http://www.cnblogs.com/xpchild/p/3825309.html 
- linux 内核之旅
			http://www.kerneltravel.net/?p=534 http://mp.weixin.qq.com/mp/homepage?__biz=MzI3NzA5MzUxNA==&hi ... 
