Only a handful of source code lines is required to make a JAXB Marshaller object write a document tree as an XML file. First you obtain a Marshaller from a JAXBContext. Then, you might set a number of properties, such as the one that's used below, which requests nice formatting of the XML text. Other properties concern the inclusion of a schema location as an attribute in the top-level element, or the encoding in the XML prolog. The first argument must be an object that is either a root element, as defined by your schema, or a JAXBElement<?>.

import java.io.*;
import javax.xml.bind.* void writeDocument( Object document, String pathname )
throws JAXBException, IOException {
Class<T> clazz = document.getValue().getClass();
JAXBContext context = JAXBContext.newInstance( clazz.getPackage().getName() );
Marshaller m = context.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
m.marshal( document, new FileOutputStream( pathname ) );
}

Sometimes marshalling needs to be done not only for one or two root documents but for objects of many different schema types. You could, of course, add xsd:element definitions for all of them to the top level xsd:schema element, but this is cumbersome. A generic solution is presented below. The method wraps an arbitrary element of some type T into a JAXBElement<T>.

<T> JAXBElement<T> wrap( String ns, String tag, T o ){
QName qtag = new QName( ns, tag );
Class<?> clazz = o.getClass();
@SuppressWarnings( "unchecked" )
JAXBElement<T> jbe = new JAXBElement( qtag, clazz, o );
return jbe;
}

To use it, you must create a context capable of handling all the classes that might crop up as instantiations for T. (Creating a JAXBContext for several packages or classes is explained in section The JAXB Context.) With a Marshaller m obtained from this context, the application of wrap is as simple as this:

SomeType st = ...;
JAXBElement<SomeType> jbx = wrap( "http://www.acme.com", "someTag", st );
m.marshal( jbx, System.out );

JAXB - Calling marshal的更多相关文章

  1. Table of Contents - JAXB

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

  2. [转]Passing Managed Structures With Strings To Unmanaged Code Part 1

    1. Introduction. 1.1 Managed structures that contain strings are a common sight. The trouble is that ...

  3. jaxb

    一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...

  4. Jaxb annotation使用

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...

  5. JAXB最佳实践

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

  6. XStream、JAXB 日期(Date)、数字(Number)格式化输出xml

    XStream.Jaxb是java中用于对象xml序列化/反序列化 的经典开源项目,利用它们将对象转换成xml时,经常会遇到日期(Date).数字按指定格式输出的需求,下面是使用示例: 一.日期字段格 ...

  7. java JAXB 学习

    JAXB(Java Architecture for XML Binding)是JDK的一部分,用于Object <-> XML的转换(有点类似于.NET中的XML序列化). 1.创建XS ...

  8. JAXB玩转命名空间

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

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

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

随机推荐

  1. Turn Your Raspberry Pi Into a WiFi Hotspot with Edimax Nano USB EW-7811Un (RTL8188CUS chipset)

    http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un- ...

  2. Django中的Model(字段)

    Model Django中的model是用来操作数据库的,Model是一个ORM框架,我们只需要关心model的操作,而不需要关心到底是哪一种数据库. 一.基本知识: 数据库引擎: Django中自带 ...

  3. 【现代程序设计】【homework-03】【11061027】

    Q:你现在使用的代码规范是什么,  和上课前有什么改进? A: 代码规范........自定义的代码规范算 代码规范吗.....   Q:你的同伴有哪些优点 (列出至少三点), 和那些需要改进的地方 ...

  4. elecworks 报表----按线类型的电线清单

    按线类型的电线清单中:列Wire number指的是线标注,不是电位标注 section:截面积

  5. elecworks 图框管理器

    图框管理器中存储的是图纸模板(图框),新建图框的步骤如下: 1 数据库---图框管理器----新建 2 打开图框属性设置窗口,设置图框属性,设置好之后点击确定 3 右击图框图标---打开(进入图框绘制 ...

  6. [OC Foundation框架 - 4] NSString的导出

      void exportString() { NSString *str = @"Hello, 坑爹"; NSString *path = @"/Users/hello ...

  7. like用法

    SQL:btitle like '%"+keyword+"%' 存储过程:keyword like ''%'+@keyword+'%'' 直接查找:name like '%wang ...

  8. MFC版美女找茬

    今天心情:捡了个闲暇. 前几天工作出了个漏洞,电话会议时候怎么都是忽大忽小的声音,实在没听清电话会议的内容,完了依据想象交了一个设计方案,之后便是赋闲. 进入正题,美女找茬实现不难,没有设计上的难度, ...

  9. 教你50招提升ASP.NET性能(十):减少通过网络发送的数据

    (16)Reduce the data sent across the network 招数16: 减少通过网络发送的数据 Reducing the amount of data sent acros ...

  10. 【转】MyEclipse8.5集成Tomcat7时的启动错误:Exception in thread “main” java.lang.NoClassDefFoundError org/apache/commons/logging/LogFactory

    http://www.cnblogs.com/newsouls/p/4021198.html 今天,安装Tomcat7.0.21后,单独用D:\apache-tomcat-7.0.21\bin\sta ...