[英]Jaxb not unmarshalling xml
I try to unmarshall one xml to ArrayList or to Class but the object which i make is not populated by JAXB 我尝试将一个xml解组到ArrayList或者Class,但是我创建的对象没有被JAXB填充
@Override
public Ships load(String xmlFilePath) {
//Ships ship = new Ships();
ArrayList<Ship> shps = new ArrayList<>();
try {
File file = new File(xmlFilePath);
JAXBContext jaxbContext = JAXBContext.newInstance(Ship.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
shps = (ArrayList<Ship>) jaxbUnmarshaller.unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
return shps;}
}
Maybe you already read this solution somewhere else, but the most common solution is to make a wrapper class (or even better, a generic, reusable wrapper class). 也许您已经在其他地方读过这个解决方案,但最常见的解决方案是创建一个包装类(或者更好的是,一个通用的,可重用的包装类)。 In the code below there's a working example of this concept, I hope the code is self explanatory. 在下面的代码中有一个这个概念的工作示例,我希望代码是自我解释的。
package com.quick;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) // just to avoid the get and set methods
class Wrapper<T> {
@XmlAnyElement
List<T> list;
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) // just to avoid the get and set methods
class Ship {
String id = "N" + System.nanoTime();
}
public class Main {
public static void main(String[] args) throws JAXBException {
// context, marshaller and unmarshaller
JAXBContext context = JAXBContext.newInstance(Ship.class, Wrapper.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Unmarshaller unmarshaller = context.createUnmarshaller();
// original list
List<Ship> originalList = new ArrayList<>();
originalList.add(new Ship());
originalList.add(new Ship());
originalList.add(new Ship());
// wrapper object
Wrapper<Ship> originalWraper = new Wrapper<>();
originalWraper.list = originalList;
// marshal the original wrapper and save the bytes in byteArray1
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
marshaller.marshal(originalWraper, os1);
byte[] byteArray1 = os1.toByteArray();
// unmarshall the bytes
ByteArrayInputStream is = new ByteArrayInputStream(byteArray1);
Wrapper<Ship> unmarshaledWrapper = (Wrapper<Ship>) unmarshaller.unmarshal(is);
// THIS LIST SHOULD CONTAIN A COPY OF originalList
List<Ship> unmarshaledList = unmarshaledWrapper.list;
// marshal the unmarshaledWrapper (...) and sabe the bytes in byteArray2
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
marshaller.marshal(unmarshaledWrapper, os2);
byte[] byteArray2 = os2.toByteArray();
// print the bytes, they should be the same
System.out.println(new String(byteArray1));
System.out.println(new String(byteArray2));
}
}
The general idea is to make a class that works as a wrapper for your list (or array) that can be marshaled or marshaled according to your needs. 一般的想法是创建一个类作为列表(或数组)的包装器,可以根据您的需要进行编组或编组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.