JAXB - Hello World with Namespace
如果元素带有命名空间,那么处理方式与 JAXB - Hello World 会略有不同。
1. XML Schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:ns0="http://www.huey.com/hello/beans/" targetNamespace="http://www.huey.com/hello/beans/" jxb:version="2.0">
<xsd:element name="Greetings" type="ns0:GreetingListType"/>
<xsd:complexType name="GreetingListType">
<xsd:sequence>
<xsd:element name="Greeting" type="ns0: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>
2. 编译 schema 生成 Java 类:
xjc hello.xsd // 取默认的包路径,根据命名空间生成
3. 修改生成的 GreetingListType.java 文件,添加 @XmlRootElement,指定根元素的名称和命名空间:
package com.huey.hello.beans; 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.XmlRootElement;
import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GreetingListType", propOrder = {
"greeting"
})
@XmlRootElement(namespace = "http://www.huey.com/hello/beans/", name = "Greetings")
public class GreetingListType { @XmlElement(name = "Greeting", required = true)
protected List<GreetingType> greeting; public List<GreetingType> getGreeting() {
if (greeting == null) {
greeting = new ArrayList<GreetingType>();
}
return this.greeting;
} }
4. marshal:
public class JaxbUtils {
// ...
public static <T> String marshal(T obj) {
StringWriter writer = new StringWriter();
try {
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller m = jc.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
m.marshal(obj, writer);
} catch (JAXBException jbe) {
// ...
}
return writer.toString();
}
}
5. 测试 JaxbUtils.marshal 方法:
public class JaxbUtilsTest {
@Test
public void testMarshal() throws Exception {
GreetingListType glt = new GreetingListType();
GreetingType gt1 = new GreetingType();
gt1.setLanguage("en");
gt1.setText("Hello world");
glt.getGreeting().add(gt1);
GreetingType gt2 = new GreetingType();
gt2.setLanguage("fr");
gt2.setText("Bonjour, madame");
glt.getGreeting().add(gt2);
System.out.println(JaxbUtils.marshal(glt));
}
}
6. JaxbUtils.marshal 测试输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Greetings xmlns:ns2="http://www.huey.com/hello/beans/">
<Greeting language="en">
<Text>Hello world</Text>
</Greeting>
<Greeting language="fr">
<Text>Bonjour, madame</Text>
</Greeting>
</ns2:Greetings>
7. unmarshal:
public class JaxbUtils {
// ...
public static <T> T unmarshal(Class<T> docClass, InputStream inputStream) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(docClass);
Unmarshaller u = jc.createUnmarshaller();
T obj = (T) u.unmarshal(inputStream);
return obj;
}
}
8. 以 JaxbUtils.marshal 的测试输出作为 JaxbUtils.unmarshal 的输入做测试:
public class JaxbUtilsTest {
@Test
public void testUnmarshal() throws Exception {
String path = "/files/hello.xml";
InputStream in = JaxbUtilsTest.class.getResourceAsStream(path);
try {
GreetingListType greetingList = JaxbUtils.unmarshal(GreetingListType.class, in);
List<GreetingType> greetings = greetingList.getGreeting();
for (GreetingType greeting : greetings) {
System.out.println(greeting.getLanguage() + ": " + greeting.getText());
}
} catch (JAXBException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(in);
}
}
}
9. JaxbUtils.unmarshal 测试输出:
fr: Bonjour, madame
en: Hey, you
Namespace
JAXB - Hello World with Namespace的更多相关文章
- JAXB最佳实践
JAXB主要用来实现对象和XML之间的序列化和反序列化. 本文主要总结JAXB基本使用方法和注意事项! 通过下文的XML示例内容进行JAXB的简单实践 <?xml version="1 ...
- JAXB玩转命名空间
声明:如果你正在发愁xml命名空间及其前缀问题,那么请继续,否则请跳过 本文讲解使用jaxb结合dom4j的XMLFilterImpl过滤器实现序列化和反序列化的完全控制 主要实现以下功能 序列化及反 ...
- JAXB命名空间及命名空间前缀处理
本篇介绍下JAXB进阶使用,命名空间处理 使用package-info.java添加默认命名空间在需要添加命名空间的包下面添加package-info.java文件,然后添加@XmlSchema注解, ...
- JAX-WS:背后的技术JAXB及传递Map
转载:http://www.programgo.com/article/98912703200/ 1.什么是JAX-WS JAX-WS (JavaTM API for XML-Based Web Se ...
- JAXB注解【转】
http://blog.csdn.net/lw371496536/article/details/6942045 JAXB(Java API for XML Binding),提供了一个快速便捷的方式 ...
- Table of Contents - JAXB
Getting Started Hello World Hello World with Namespace xjc - 将 XML Schema 编译成 Java 类 wsimport: 编译 WS ...
- JAXB - The JAXB Context
As we have seen, an object of the class JAXBContext must be constructed as a starting point for othe ...
- JAXB - Annotations, Top-level Elements: XmlRootElement
A class that describes an XML element that is to be a top-level element, i.e., one that can function ...
- JAXB - Annotations, The Object Factory: XmlRegistry, XmlElementDecl
To be able to create objects from XML elements, the unmarshaller must have an object factory with me ...
随机推荐
- matlab 函数说明--waitforbuttonpress
这个函数的名称取得不是太好,一眼看去,好像是等待按键的意思,但是实际上它等待的是按键或者鼠标事件,他的功能描述如下: 停止脚本的执行,直至当前活动的窗口上检测到了鼠标按下事件或者有效的键盘事件(有效是 ...
- SGU132 - Another Chocolate Maniac(状态压缩DP)
题目大意 给定一个N*M大小的大小的蛋糕,蛋糕的有些地方已经放置了东西,要求你在蛋糕上放入尽量少的1*2大小的巧克力,使得蛋糕不能够再放入巧克力 题解 和POJ1038恰好相反,此题是放入尽量少的巧克 ...
- Sublime Text 2结合VS2010配置C C++编译
本文参考以下文章 特此谢谢 http://www.cnblogs.com/akira90/archive/2013/01/02/2842571.html 因遇到错误,浪费一个小时才解决 一.利用VS2 ...
- Java之字节输入流和输出流
package IODemo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...
- ALM11用例测试类型
默认一般都是选择的是manual
- Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性。
Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性. 懒加载属性 Swift在语言层面上提供了类中懒加载属性的支持,使用lazy作为关键字: class Renderer { lazy v ...
- [css]inline-block
能被父容器居中.能设置高度宽度和margin.不会像table或div那样占一正行……——这就是inline-block——记得这是浏览器默认样式告诉你的.
- UINavgation日常小bug-有兴趣的朋友可以看看
UINavgation日常 UINavgation 今天在做一个小Demo,发现一个Bug,挺有意思的,就是在你不断调用Navigation- (void)pushViewController:(UI ...
- UIDynamic(一)
UIDynamic(一) 前言 最近看了一下UIDynamic,UIDynamic是13年WWDC出的技术.其实本人一直热衷于比较有趣的动画,特别是带物理力学的动画,感觉物理力学就是动画的灵魂,一直想 ...
- android studio里的build.gradle基本属性
//声明是android 程序 apply plugin: 'com.android.application' android { //编译SDK版本 compileSdkVersion 23 // ...