首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助解决JAXB unmarshall问题

需要帮助解决JAXB unmarshall问题
EN

Stack Overflow用户
提问于 2022-11-21 21:21:47
回答 1查看 17关注 0票数 0

我有一个典型的‘意外元素’(uri:"",local:“粘贴注释”)‘JAXB错误,尽管它只在我在解封组对象上设置事件处理程序时显示。当未设置事件处理程序时,代码运行良好,但所需的特定数据不会被解除封送。

我在这个问题上做了几天的工作,包括很多互联网搜索,但没有找到正确的解决方案。我觉得我错过了一些简单的东西,但到目前为止,它还在逃避我。

备注:

  1. 我正在使用Java 8和Apache 15。
  2. 原始代码是用xjc命令从多个供应商收到的模式生成的。我不能修改模式,必须弄清楚如何“按原样”使用它们。
  3. 我编写了下面的代码来重复这个问题并简化发布的代码。为了简洁起见,已删除导入语句。如果需要,请告诉我。
  4. ,因为其中一个类涉及继承,所以我添加了适当的@XmlSeeAlso注释。
  5. ,这是我第一次真正进入JAXB。这似乎是这个项目的完美选择。我只需要想办法让它起作用。

首先,示例XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<documents>
    <document>
        <notes>
            <stickynote id="123">test note</stickynote>
        </notes>
    </document>
</documents>

创建JAXB上下文和创建解组对象的代码:

代码语言:javascript
复制
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 );

处理每个元素的相应类:

文档类

代码语言:javascript
复制
@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;
    }
}

文档类

代码语言:javascript
复制
@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类

代码语言:javascript
复制
@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类

代码语言:javascript
复制
@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类:

代码语言:javascript
复制
@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;
    }
}

例外是:

代码语言:javascript
复制
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*

谢谢您的任何建议和帮助!

约翰

EN

回答 1

Stack Overflow用户

发布于 2022-11-22 21:56:19

我的问题是我是如何使用xjc的。我正在构建我们单独收到的每个模式,但没有意识到由于供应商使用其他供应商模式,模式中存在一些重叠。当一个供应商的模式在流程的早期构建,然后在使用早期供应商的模式之后构建另一个供应商的模式时,就会产生问题。

将所有模式构建为一个组,而不是单独构建,允许xjc“查看”它需要看到的所有内容,并使相应的代码按预期工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74525027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档