JAXB - Calling marshal

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 );
时间: 2024-07-30 16:06:04

JAXB - Calling marshal的相关文章

Java Jaxb JavaBean与XML互转

1.Jaxb - Java Arcitecture for XML Binding 是业界的一个标准,是一项可以根据XML Schema产生Java类的技术. Jaxb2.0是Jdk1.6的组成部分.不需要在第三方Jar包的支持下即可完成Xml与JavaBean的相互转换. 2.重要概念: ·JAXBContext类,是应用的入口,用于管理XML/Java绑定信息. ·Marshaller接口,将Java对象序列化为XML数据. ·Unmarshaller接口,将XML数据反序列化为Java对象

利用JAXB实现java实体类和xml互相转换

1.应用场景 在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换 2.Demo 2.1 student.java 实体类,包含list(set同理).map.Teacher.Date 类型的属性 package jaxb; import java.util.Date; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.XmlAccess

JAXB - Hello World with Namespace

如果元素带有命名空间,那么处理方式与 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=

JAXB最佳实践

JAXB主要用来实现对象和XML之间的序列化和反序列化. 本文主要总结JAXB基本使用方法和注意事项! 通过下文的XML示例内容进行JAXB的简单实践 <?xml version="1.0" encoding="UTF-8"?> <Provinces> <Province id="B001"> <name>北京</name> <code>30000</code>

[CXF REST标准实战系列] 一、JAXB xml与javaBean的转换

Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.不认识到犯错,然后得到永久的教训. 2.认识JAXB 3.代码实战 1.不认识到犯错,然后得到永久的教训. 也不是所谓的教训吧,真正的教训来自于对错误的剖析理解很深刻.然后有种"吃一堑,长一智"的感觉才叫教训.近日和团队工头们用CXF3.0和Spring4.0开发一个平台,模仿着第三方支付,用xml进行数据交互

Calling C++ code from C# z

http://blogs.msdn.com/b/borisj/archive/2006/09/28/769708.aspx I apologize for the long delay for this section (although I suppose my average posting frequency is already pretty low), but I was on a much needed vacation. I finished the last chapter wi

XML编程总结(六)——使用JAXB进行java对象和xml格式之间的相互转换

(六)使用JAXB进行java对象和xml格式之间的相互转换 JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类. JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向生成Java对象树

使用JAXB来实现Java合xml之间的转换

使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了. //javaBean-->xml @Test public void test1() { try { JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Marshaller marshaller = jaxbContext.createMarshaller(); User user1 = new User("张三", "zh

Jaxb笔记

摘自: http://www.blogjava.net/eagle-daiq/archive/2012/01/30/369016.html 最近项目原因,研究了下jaxb.jaxb是Java api xml binding的简称,是为实现java与xml数据的相互转换而定义的一个api标准.该标准以annotation的方式实现xml的转换.不用开发人员单独解析每个对象属性与xml元素的mapping关系,只需在java bean中注入简单的java annotation,其他的交给工具去处理.