如果元素带有命名空间,那么处理方式与 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的更多相关文章

  1. JAXB最佳实践

    JAXB主要用来实现对象和XML之间的序列化和反序列化. 本文主要总结JAXB基本使用方法和注意事项! 通过下文的XML示例内容进行JAXB的简单实践 <?xml version="1 ...

  2. JAXB玩转命名空间

    声明:如果你正在发愁xml命名空间及其前缀问题,那么请继续,否则请跳过 本文讲解使用jaxb结合dom4j的XMLFilterImpl过滤器实现序列化和反序列化的完全控制 主要实现以下功能 序列化及反 ...

  3. JAXB命名空间及命名空间前缀处理

    本篇介绍下JAXB进阶使用,命名空间处理 使用package-info.java添加默认命名空间在需要添加命名空间的包下面添加package-info.java文件,然后添加@XmlSchema注解, ...

  4. JAX-WS:背后的技术JAXB及传递Map

    转载:http://www.programgo.com/article/98912703200/ 1.什么是JAX-WS JAX-WS (JavaTM API for XML-Based Web Se ...

  5. JAXB注解【转】

    http://blog.csdn.net/lw371496536/article/details/6942045 JAXB(Java API for XML Binding),提供了一个快速便捷的方式 ...

  6. Table of Contents - JAXB

    Getting Started Hello World Hello World with Namespace xjc - 将 XML Schema 编译成 Java 类 wsimport: 编译 WS ...

  7. JAXB - The JAXB Context

    As we have seen, an object of the class JAXBContext must be constructed as a starting point for othe ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Debug Assertion Failed!

    问题并没有解决..... 不知道怎么回事,先都没有这样的情况... VC++调程序出现如下错误: Debug   Assertion   Failed!       Program:   D:wyuS ...

  2. Storm入门

    Storm框架入门 1 Topology构成 和同样是计算框架的Mapreduce相比,Mapreduce集群上运行的是Job,而Storm集群上运行的是Topology.但是Job在运行结束之后会自 ...

  3. HUOJ-10857 最大的面积 凸包+DP

    题目链接:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10857&courseid=55 比赛的时候 ...

  4. hdu4435-charge-station(搜索+贪心)

    题意&题解: http://www.cnblogs.com/wuminye/p/3245546.html 说实话看了题解觉得很简单,但是比赛的时候真的是毫无头绪. 然而印象中做过一道类似的二进 ...

  5. 设置Windows 远程协助与远程桌面

    家庭局域网组建完成后,即可通过远程协助解决各种问题,或联机玩游戏等. 使用Windows 7\8\10 远程协助与远程桌面 Windows 8系统中自带了远程协助功能,家庭用户只需要做简单的设置,就可 ...

  6. SHH入门:Spring框架简介

    (1)Spring 七大模块 核心容器:核心容器提供Spring 框架的基本功能.核心容器的主要组件是 BeanFactory,它是工厂模式的实现.BeanFactory 使用控制反转 (IOC) 模 ...

  7. Python Django manage.py提供的命令及用法

    λpython manage.pyType'manage.py help <subcommand>'for help on a specific subcommand. Available ...

  8. JQuery上传插件Uploadify API详解

    一.相关key值介绍uploader:uploadify.swf文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击后淡出打开文件对话框,默认值:uploadify.swf. scrip ...

  9. cocos2dx中android下动态更新.so文件

    作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037595.html  因为没用lua脚本写游戏,所以每次发布出去后,发现在bug,需要 ...

  10. 【24】若所有参数皆需类型转换,请为此采用non-members函数

    1.令class支持隐式类型转换,往往是个糟糕的主意.但有些情况是合理的,比如数值类型.考虑,有理数Rational有分子,分母两个字段,缺省参数值为0,1.Ration a = 2;我们期望构造一个 ...