Jaxb如何在Java中按声明的类型解组?
我尝试用JAXB在JAVA中解压一个简单的xml文档:Jaxb如何在Java中按声明的类型解组?,java,xml,jaxb,unmarshalling,Java,Xml,Jaxb,Unmarshalling,我尝试用JAXB在JAVA中解压一个简单的xml文档: <?xml version="1.0" encoding="UTF-8"?> <root> <fruit>banana</fruit> <fruit>apple</fruit> <vegetable>tomatoe</vegetable> <vegetable>potatoe</vegetab
<?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>
香蕉
苹果
汤姆托
波塔托
松果体
甘蓝
胡萝卜
草莓
我不明白你的工作方法
unmarshal <T> JAXBElement<T> unmarshal(Node node,Class<T> declaredType) throws JAXBException
unmarshal JAXBElement unmarshal(节点节点,类declaredType)抛出jaxbeexception
因为对于我的问题,我必须用JAXB和解组来表示所有“根”子级。
我需要将这个xml解组为java对象
感谢之前为我的问题花费的时间。为了从根元素中解组,只需:
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
JAXBContext-JAXBContext=JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarhsaller=jaxbContext.createUnmarshaller();
Root=(Root)unmarhsaller.unmarshal(新文件(“File.xml”);
//获取水果节点列表(假设已创建XML文档对象模型)
List fruit=root.getFruit();
//在这里做你的事