【问题标题】:XML does marshal properly, does not unmarshal because XML doc is declared invalidXML 正确编组,不解组,因为 XML 文档被声明为无效
【发布时间】:2013-05-25 18:05:47
【问题描述】:

我正在尝试重新读取由我的 Java 程序生成的 XML 文件,并以 JTable 形式提供它的图形表示。手动生成的 XML 符合架构,但程序将其检测为无效。

逻辑很简单:
1.检查task-list.xmltask-list-schema.xsd是否存在。
2. 如果,解组 XML,使用 XML 文档中的数据准备行,将行添加到表中。
3.如果,准备一个空白的GUI。

问题在于 XML 不符合架构。问题不在生成的 XML 或架构中它在用于绑定的类中。它们是这样的:

FormatList
|->Vector<Format>

TaskList
|-> Vector<Task>

Task
|-> input xs:string
|-> output xs:string
|-> Format 
|-> taskID xs:integer
|-> isReady xs:boolean  

Format
|-> name xs:string
|-> width xs:string
|-> height xs:string
|-> extension xs:string

所以,FormatListTask 都共享同一个类Format,因为每个视频转换任务都有与之关联的格式。

这是我得到的错误:

这是生成的 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<task-list>
    <task>
        <input>E:\Videos\AutoIT\AutoIt Coding Tutorial Two - Website Functions.flv</input>
        <output>E:\test\StandaloneVideoConverter</output>
        <format>
            <name>[AVI] HD 1080p</name>
            <width>1920</width>
            <height>1080</height>
            <extension>.avi</extension>
        </format>
        <taskID>3</taskID>
        <isReady>false</isReady>
    </task>
</task-list>  

我该如何解决?

@XmlAccessorType(XmlAccessType.FIELD)
public class Format {
    @XmlElement(name="name")
    private String name;
    @XmlElement(name="width")
    private int width;
    @XmlElement(name="height")
    private int height;
    @XmlElement(name="extension")
    private String extension;

    //getters and setters, synchronized
}

@XmlRootElement(name="format-list")
@XmlAccessorType(XmlAccessType.FIELD)
public class FormatList {
    @XmlElement(name="format")
    private Vector<Format> formats;


    public Vector<Format> getFormats(){
        return formats;
    }
    // this is the complete class
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Task {
    @XmlElement(name="input")
    private String input;   // String representing the input file
    @XmlElement(name="output")
    private String output; // String representing the output file
    @XmlElement(name="format")
    private Format format; // a jaxb.classes.Format representing the format of conversion
    @XmlElement(name="taskID")
    private long taskID; // a unique ID for each task.
    @XmlElement(name="isReady")
    private boolean isReady; // boolean value representing whether the task is ready for conversion

    @XmlTransient
    private boolean isChanging = false; // boolean representing if the user is changing the task DO NOT MARSHALL
    @XmlTransient
    private boolean isExecuting = false; // boolean representing whether the task is being executed  DO NOT MARSHALL



    // getters and setters, synchronized
}

@XmlRootElement(name="task-list")
@XmlAccessorType(XmlAccessType.FIELD)
public class TaskList {

    public TaskList(){
        tasks = new Vector<Task>();
    }

    @XmlElement(name="task")
    Vector<Task> tasks;

    public Vector<Task> getTasks(){
        return tasks;
    }

    // this is  the complete class

}

【问题讨论】:

  • 如果需要任何其他信息,请告诉我:)
  • 你能提供课程吗?
  • 按要求完成。为了清除混乱,我排除了 getter 和 setter 方法:)
  • 感谢您添加课程。 @RyanStewart 给出的答案似乎是正确的。

标签: java xml jaxb unmarshalling


【解决方案1】:

错误似乎与您发布的 XML 不匹配。该错误表示 JAXB 正在尝试解组 format-list 元素,但不知道如何处理它。该 XML 中没有 format-list。根据给定的错误,我希望您有这样的代码:

JAXBContext.newInstance(TaskList.class).createUnmarshaller().unmarshal(xml);

你给它 FormatList XML 而不是 TaskList XML 作为输入。

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2011-10-18
    • 1970-01-01
    相关资源
    最近更新 更多