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. javscript面试题(一)

    你如何理解HTML结构的语意化? 1.去掉或样式丢失的时候能让页面呈现清晰的结构:2.屏幕阅读器(如果访客有视障)会完全根据你的标记来“读”你的网页:3.PDA.手机等设备可能无法像普通电脑的浏览器一 ...

  2. sql-定义变量

    declare @subject nvarchar(50) set @subject=(select Subject from dbo.Scores where ID=1) --select @sub ...

  3. 【Kafka入门】Kafka基础结构和知识

    基本概念的总结 在基本的Kafka架构中,producer将消息发布到Kafka话题中,一个Kafka话题通常指消息的类别或者名称,Kafka话题被集群中一个充当Kafka server角色的 bro ...

  4. 100% opacity UILabel over a 50% opacity background (UIView?) UIView是百分之50透明而上面的UILable是100%不透明

    So right now I have a UIView with a UILabel in it. I want the background to have an opacity < 1.0 ...

  5. 射频识别技术漫谈(8)——动物标签【worldsing笔记】

    动物标签也是工作在TTF模式的ID(Identification)卡.之所以通常称为动物标签,估计是因为一来和识别人的ID卡相区分,二是因为动物不如人听话,人的ID卡可以做成卡片形状拿在手上,而动物不 ...

  6. 求前几日的平均值用obj.reduce()方法

    const average = data=>data.map((item, idx, origin)=>Math.round(origin.slice(0,idx+1).reduce((a ...

  7. 【Away3D代码解读】(三):渲染核心流程(渲染)

    还是老样子,我们还是需要先简略的看一下View3D中render方法的渲染代码,已添加注释: //如果使用了 Filter3D 的话会判断是否需要渲染深度图, 如果需要的话会在实际渲染之前先渲染深度图 ...

  8. ASP.NET MVC- EF返回连接池用ADO.NET方式访问数据库

    用习惯了ADO.NET的方式去访问数据库,虽然ADO.NET写的代码没有EF简洁,可是也并不麻烦.而且EF在进行多表查询的那种方式是,EF需要先去数据库里定义外键,再进去一次代码生成,然后才能用INC ...

  9. ListView Video

    com.baidu.frontia.FrontiaApplication ListView Video <item name="android:windowBackground&quo ...

  10. C#系列之String和StringBuilder

    前言 首先和博园的各位打声招呼,小弟在博园呆了也有一年多了.平常一有时间就会过来看看文章,学习各位的经验,现在养成了一种一天不来博园,心里就不踏实的习惯,不知道在看我博文的大哥们有没有这个感觉. 读归 ...