JAXB UnmarshallException 意外元素但 "Expected elements are (none)"
JAXB UnmarshallException unexpected element but "Expected elements are (none)"
我尝试制作一个非常 'abstract' 的方法,使用 JAXB (javax.xml.bind.*
) 将任何类型的对象转换为 XML-String,反之亦然。
我收到一个非常奇怪的错误,我不知道它的含义。
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Incident"). Expected elements are (none)
我在 google 和 Whosebug 上搜索了很多解决方案,但他们的解决方案似乎没有帮助。我在这里面临死胡同。
我的转换器方法
public Object convertXmlToObject(String string, Class c) throws ConversionException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(c.getClass());
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStream stream = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
Object converted = jaxbUnmarshaller.unmarshal(stream);
return converted;
} catch (JAXBException e) {
e.printStackTrace();
throw new ConversionException("Could not convert the message to an Object", e);
}
}
我调用方法的地方
public void generateIncidentReport(Incident incident) throws RepositoryException, ConversionException {
ConversionTool conversionTool = new Converter();
String xmlMessage = conversionTool.convertObjectToXml(incident);
//...
}
我的事件class(包含所有需要的注释)
@XmlRootElement(name = "Incident")
@XmlAccessorType(XmlAccessType.FIELD)
public class Incident {
@XmlElement(name = "shipId")
private int shipID;
@XmlElement(name = "incidentType")
private String type;
@XmlElement(name = "action")
private String action;
@XmlElement(name = "centraleID")
private String centraleID;
@XmlElement(name = "Ship")
private Ship ship;
public Incident() {
}
//getters and setters
}
最后 XML 字符串
<Incident><incidentType>Medisch noodgeval</incidentType><shipId>1234567</shipId></Incident>
你写
JAXBContext jaxbContext = JAXBContext.newInstance(c.getClass());
c
已经是 class,因此为 java.lang.Class
创建上下文。你需要的是
JAXBContext jaxbContext = JAXBContext.newInstance(c);
我尝试制作一个非常 'abstract' 的方法,使用 JAXB (javax.xml.bind.*
) 将任何类型的对象转换为 XML-String,反之亦然。
我收到一个非常奇怪的错误,我不知道它的含义。
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Incident"). Expected elements are (none)
我在 google 和 Whosebug 上搜索了很多解决方案,但他们的解决方案似乎没有帮助。我在这里面临死胡同。
我的转换器方法
public Object convertXmlToObject(String string, Class c) throws ConversionException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(c.getClass());
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStream stream = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
Object converted = jaxbUnmarshaller.unmarshal(stream);
return converted;
} catch (JAXBException e) {
e.printStackTrace();
throw new ConversionException("Could not convert the message to an Object", e);
}
}
我调用方法的地方
public void generateIncidentReport(Incident incident) throws RepositoryException, ConversionException {
ConversionTool conversionTool = new Converter();
String xmlMessage = conversionTool.convertObjectToXml(incident);
//...
}
我的事件class(包含所有需要的注释)
@XmlRootElement(name = "Incident")
@XmlAccessorType(XmlAccessType.FIELD)
public class Incident {
@XmlElement(name = "shipId")
private int shipID;
@XmlElement(name = "incidentType")
private String type;
@XmlElement(name = "action")
private String action;
@XmlElement(name = "centraleID")
private String centraleID;
@XmlElement(name = "Ship")
private Ship ship;
public Incident() {
}
//getters and setters
}
最后 XML 字符串
<Incident><incidentType>Medisch noodgeval</incidentType><shipId>1234567</shipId></Incident>
你写
JAXBContext jaxbContext = JAXBContext.newInstance(c.getClass());
c
已经是 class,因此为 java.lang.Class
创建上下文。你需要的是
JAXBContext jaxbContext = JAXBContext.newInstance(c);