如果元素带有命名空间,那么处理方式与 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. Bzoj 4403: 序列统计 Lucas定理,组合数学,数论

    4403: 序列统计 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 328  Solved: 162[Submit][Status][Discuss] ...

  2. openstack学习线路指导

    原文链接: http://www.aboutyun.com/thread-7225-1-1.html 网上很多hadoop资料,openstack资料相对较少,这里整理一下,帮助初学者尽快入门. 首先 ...

  3. sqlite 修改表名,合并数据库(文件)

    修改表名:ALTER TABLE orig_table_name RENAME TO tmp_table_name; 将某个数据库的一个表的数据插入到另一个数据库的某个表里:1.先连接数据库A2.再a ...

  4. jad的用法(反编译某目录下所有class)

    jad -s java -d E:\scm\MonitorServerEx\src2 -o -ff -r E:\scm\MonitorServerEx\classes-recomp\**\*.clas ...

  5. Java HashMap 源码解析

    今天正式开始分析具体集合类的代码,首先以既熟悉又陌生的HashMap开始. 签名(signature) public class HashMap<K,V> extends Abstract ...

  6. EntityFramework更新多条数据【8万】

    此文主要用做记录用: 原因:数据库迁移,需要转换大量用户资料,两数据某字段加密方式不一致需要批量转换 注:转换程序用了EntityFramework 过程: 1.读取所有需要转换数据至List 2.采 ...

  7. 百度oauth2.0 WEB 链接

    Source:http://developer.baidu.com/wiki/index.php?title=docs/oauth/authorization Webpage Function : A ...

  8. Android实例-TRectangle加载图片(XE8+小米2)

    结果: 1.加载图片很流畅,可以做背景用. 2.现在是加载了正形与圆形,其他形状能不能加载呢?自己测试哦,要多动手才行. 3.需要把图片打到包里哦(路径为“assets\internal\”). 实例 ...

  9. SQL2008-不同数据库之间的触发器

    create trigger tr_update_Table_1   on   rwqd     FOR  UPDATE   As   update dataabc.dbo.Table_1  set ...

  10. STL学习系列二:Vector容器

    1.Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添 ...