zoukankan      html  css  js  c++  java
  • JAXB

    A simple approach for unmarshalling an XML document consists of the creation of a JAXB context and the call to unmarshal the document. A JAXBContext object provides the entry point to the JAXB API and maintains the binding information between XML and Java. One way of creating a context instance is by calling the static method newInstance with a list of colon separated names of the packages containing the JAXB schema-derived classes. From this context, an Unmarshaller object is obtained, which functions as the driver for processing an XML text to create the equivalent set of Java objects. It offers several unmarshal methods, accepting a wide range of object types as the source for XML text data. The method shown below illustrates this with a single package containing the class of the type defining the top level element of the XML document.

    public <T> T unmarshal( Class<T> docClass, InputStream inputStream )
        throws JAXBException {
        String packageName = docClass.getPackage().getName();
        JAXBContext jc = JAXBContext.newInstance( packageName );
        Unmarshaller u = jc.createUnmarshaller();
        JAXBElement<T> doc = (JAXBElement<T>)u.unmarshal( inputStream );
        return doc.getValue();
    }

    The return value of the call to JAXB's unmarshal is a representation of the root node of the parsed XML document in an instance of JAXBElement<T>. If we're not interested in the tag of the root element we might just as well return the extracted content value.

  • 相关阅读:
    kettle结合MySQL生成保留最近6个月月度报告_20161009
    reduce用法
    【npm下载依赖包失败】gyp ERR! stack Error: EACCES: permission denied, mkdir问题解决方案
    【前端算法3】插入排序
    【前端算法2】快速排序
    【前端算法1】二分查找
    diy 滚动条 样式 ---- 核心代码
    [数据结构] 栈
    [数据结构] 列表
    day02 Python 运算符
  • 原文地址:https://www.cnblogs.com/huey/p/5506348.html
Copyright © 2011-2022 走看看