[英]How work Jaxb to Unmarshal by Declared Type in Java?
I have tried to unmarshal one simple xml document with JAXB in JAVA: 我试图用JAVA中的JAXB解组一个简单的xml文档:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<fruit>banana</fruit>
<fruit>apple</fruit>
<vegetable>tomatoe</vegetable>
<vegetable>potatoe</vegetable>
<fruit>pineaple</fruit>
<vegetable>cabbage</vegetable>
<vegetable>carrot</vegetable>
<fruit>strawberry</fruit>
</root>
I can't understand how work method 我不明白工作方法
unmarshal <T> JAXBElement<T> unmarshal(Node node,Class<T> declaredType) throws JAXBException
because for my issue I have to represent all "root" children with JAXB and unmarshaling. 因为对于我的问题,我必须用JAXB和取消编组来代表所有“ root”孩子。 I need to unmarshal this xml to java object.
我需要将该XML解组为Java对象。
Thanks previously for the time spend for my issue. 以前感谢您花费我的时间。
For unmarshaling from the root element just: 对于从根元素拆封只是:
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller();
Root root = (Root) unmarhsaller.unmarshal(new File("file.xml"));
//getting a list of fruit nodes (suppose you have created XML document object model)
List<Fruit> fruit = root.getFruit();
// do your stuff here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.