我有一个典型的‘意外元素’(uri:"",local:“粘贴注释”)‘JAXB错误,尽管它只在我在解封组对象上设置事件处理程序时显示。当未设置事件处理程序时,代码运行良好,但所需的特定数据不会被解除封送。
我在这个问题上做了几天的工作,包括很多互联网搜索,但没有找到正确的解决方案。我觉得我错过了一些简单的东西,但到目前为止,它还在逃避我。
备注:
@XmlSeeAlso注释。首先,示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<documents>
<document>
<notes>
<stickynote id="123">test note</stickynote>
</notes>
</document>
</documents>创建JAXB上下文和创建解组对象的代码:
JAXBContext context = JAXBContext.newInstance( Documents.class );
Unmarshaller u = context.createUnmarshaller();
u.setEventHandler(new DefaultValidationEventHandler());
JAXBElement< Documents > root =
u.unmarshal(
new StreamSource( new File( FILENAME )),
Documents.class );处理每个元素的相应类:
文档类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "document" })
@XmlRootElement(name = "documents", namespace = "http://www.example.org/documents")
public class Documents {
protected Document document;
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
}文档类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "notes" })
@XmlRootElement(name = "document", namespace = "http://www.example.org/documents")
public class Document {
protected NotesType notes;
public NotesType getNotes() {
return notes;
}
public void setNotes(NotesType notes) {
this.notes = notes;
}
}NotesType类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NotesType", propOrder = { "notes" })
public class NotesType {
protected List<NoteType> notes;
public List<NoteType> getNotes() {
if ( isNull( notes )) {
notes = new ArrayList<>();
}
return this.notes;
}
}NoteType类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NoteType", propOrder = { "note" })
@XmlSeeAlso({ StickyNote.class })
public class NoteType {
@XmlAttribute(name = "id", required = true)
protected String id;
protected String note;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}最后,StickyNote类,它扩展了NoteType类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "subType" })
public class StickyNote extends NoteType {
protected String subType = "sticky";
public String getSubType() {
return subType;
}
public void setSubType(String subType) {
this.subType = subType;
}
}例外是:
DefaultValidationEventHandler: [ERROR]: unexpected element (uri:"", local:"stickynote"). Expected elements are <{}notes>
Location: line 5 of file:/C:/Test/documents.xml
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"stickynote"). Expected elements are <{}notes>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
*and so on*谢谢您的任何建议和帮助!
约翰
发布于 2022-11-22 21:56:19
我的问题是我是如何使用xjc的。我正在构建我们单独收到的每个模式,但没有意识到由于供应商使用其他供应商模式,模式中存在一些重叠。当一个供应商的模式在流程的早期构建,然后在使用早期供应商的模式之后构建另一个供应商的模式时,就会产生问题。
将所有模式构建为一个组,而不是单独构建,允许xjc“查看”它需要看到的所有内容,并使相应的代码按预期工作。
https://stackoverflow.com/questions/74525027
复制相似问题