XML确实正确编组,不会解组,因为XML文档被声明为无效

时间:2013-05-25 18:05:48

标签: java xml jaxb unmarshalling

我正在尝试重新读取由我的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,因为每个视频转换任务都有一个关联的格式。

这是我得到的错误:
enter image description here

以下是生成的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

}

1 个答案:

答案 0 :(得分:2)

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

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

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