zoukankan      html  css  js  c++  java
  • JAXB

    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 );
  • 相关阅读:
    剑指offer二十二之从上往下打印二叉树
    剑指offer二十一之栈的压入、弹出序列
    Hadoop简介与伪分布式搭建—DAY01
    getopt解析命令行参数一例:汇集多个服务器的日志
    软件开发:如何表达和维护大型逻辑
    编程语言与可复用性
    危险的 SQL
    谁终将点燃闪电,必长久如云漂泊
    如何使错误日志更加方便排查问题
    生活的诀窍:任务激励式学习法和短小目标法
  • 原文地址:https://www.cnblogs.com/huey/p/5511347.html
Copyright © 2011-2022 走看看