如果根元素唯一地对应于Java类,则将返回该类的实例,否则将返回a
JAXBElement。
如果要确保始终获取域对象的实例,则可以利用
JAXBInstrospector。下面是一个例子。
演示版
package forum10243679;import java.io.StringReader;import javax.xml.bind.*;import javax.xml.transform.stream.StreamSource;public class Demo { private static final String XML = "<root/>"; public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Root.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBIntrospector jaxbIntrospector = jc.createJAXBIntrospector(); Object object = unmarshaller.unmarshal(new StringReader(XML)); System.out.println(object.getClass()); System.out.println(jaxbIntrospector.getValue(object).getClass()); Object jaxbElement = unmarshaller.unmarshal(new StreamSource(new StringReader(XML)), Root.class); System.out.println(jaxbElement.getClass()); System.out.println(jaxbIntrospector.getValue(jaxbElement).getClass()); }}
输出量
class forum10243679.Rootclass forum10243679.Rootclass javax.xml.bind.JAXBElementclass forum10243679.Root
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)