码迷,mamicode.com
首页 > 其他好文 > 详细

Jaxb - Unmarshalling

时间:2016-05-18 21:22:14      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

Jaxb - Unmarshalling

标签:

原文地址:http://www.cnblogs.com/huey/p/5506348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!