如果元素带有命名空间,那么处理方式与 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. html5爱心表白

    http://js.itivy.com/jiaoben1892/index.html http://bangpai.sourceforge.net/main.html

  2. Sitecore Digital Marketing System, Part 1: Creating personalized, custom content for site visitors(自定义SiteCore中的 Item的Personalize的Condition) -摘自网络

    Sitecore’s Digital Marketing System (DMS) can help you personalize the content your site displays to ...

  3. delphi 调用 webservice (.NET C#版)

    uses XMLIntf, XMLDoc; XML to XTR文件转换 .File-->open打开你要分析的XML文件 .在左边选择你要分析的接点,双击加到中间的转换列表中 .Create- ...

  4. nyoj 488 素数环

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...

  5. Android 添加、移除和判断 桌面快捷方式图标

    思路: Launcher为了应用程序能够定制自己的快捷图标,就注册了一个 BroadcastReceiver 专门接收其他应用程序发来的快捷图标定制信息.所以只需要根据该 BroadcastRecei ...

  6. PowerDesigner实用技巧小结(3)

    PowerDesigner实用技巧小结(3) PowerDesigner 技巧小结 sqlserver数据库databasevbscriptsqldomain 1.PowerDesigner 使用 M ...

  7. .net 开发人员的瓶颈和职业发展

    .net 开发人员的瓶颈和职业发展 现在社会比前几年浮躁了,越来越多的人抱怨薪水低,高薪工作不好找; 诚然这有CPI的压力,可是也有很多人没有认清自己的职业发展. 很多.net程序员个各种纠结,想拿高 ...

  8. iOS新特性引导页

    有一个注意点: 获取版本号 个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击"Summary"后看到. Version在plist文件 ...

  9. mybatis 打印sql log配置

    mybatis 打印sql log, 方便调试.如何配置呢? log4j.xml : <!-- 打印sql start --> <appender name="IBatis ...

  10. 如何让OpenSSL得到JKS格式的keystore中的public and private key

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...