Jaxb - Unmarshalling

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.

时间: 2024-08-13 12:26:17

Jaxb - Unmarshalling的相关文章

JAXB XML到java object的转换

JAXB是Java Architecture for XML Binding的缩写.使用JAXB注解将Java对象转换成XML文件.在这篇教程中,我们将会展示如何使用JAXB来做以下事情: 1. marshall 将java对象转化成xml文件 2. unmarshalling 将xml内容转换成java对象 JAXB 注解(Annotation) 如果一个对象需要被转换成XML文件,或者从XML文件中生成,该对象需要用JAXB注解来标注.这些注解光凭名字就知道是什么意思了.具体可参考官网:ja

玛莎--JAXB的marshell

玛莎,安玛莎这里个方法名字好feshion,有没有玛莎拉蒂的既视感. 什么是玛 莎marshalling?就是序列化 , translating data structures or object state into a format 实现很简单,pojo对象加注解@XmlRootElement,否则会报异常 [com.sun.istack.SAXException2: 由于类型 "com.happy.entities.ItemEvaluation" 缺少 @XmlRootEleme

JAXB - Annotations, Type Adapters: XmlJavaTypeAdapter

For some Java container types JAXB has no built-in mapping to an XML structure. Also, you may want to represent Java types in a way that is entirely different from what JAXB is apt to do. Such mappings require an adapter class, written as an extensio

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对象

在Gradle中使用jaxb的xjc插件

jaxb,全称为Java Architecture for Xml Binding,是一种将java对象与xml建立起映射的技术.其主要提供两个功能,一是将java对象映射为xml,二是将xml映射为java对象.JAXB有1.0版和2.0版.2.0版对应的JSR(Java specification request, java规格要求)是JSR 222.jaxb中的xjc工具能够将XML Schema转换为对应的java类.支持的XML类型包括XML DTD,XSD以及WSDL.而schema

Android 基本 Jackson Marshalling/Unmarshalling

本文内容 基本 Jack Marshalling 忽略属性 忽略 Null 字段 改变字段名字 基本 Jackson Marshalling 把 JSON 解析成 JsonNode 带无法识别的属性的 Unmarshalling json 参考资料 术语   基本 Jackson Marshalling 如何把一个 Java 实体序列化(serialize)成一个 JSON 字符串,并且如何控制映射的过程,以便达到准确的你想要的 JSON 格式. 忽略属性 当 Jackson 默认值不够,我们就

Java操作XML的JAXB工具

在java中操作XML的工作中中,比较方便的工具是JAXB(Java Architecture for XML Binding). 利用这个工具很方便生成XML的tag和Java类的对应关系.参照网上的资料,简单说明一下java操作xml的一些东西. 1.先定义一个XML Schema文件.比如: [html] view plain copy print? <?xml version="1.0" encoding="UTF-8" standalone=&quo

利用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=