简体   繁体   English

XML确实会封送,而不会取消封送,因为XML doc被声明为无效

[英]XML does marshal properly, does not unmarshal because XML doc is declared invalid

I am trying to re-read an XML file generated by my Java program and provide a graphical representation of it in a JTable form. 我试图重新读取由Java程序生成的XML文件,并以JTable形式提供其图形表示。 The generated XML, manually, conforms to the schema however the program detects it as invalid. 手动生成的XML符合模式,但是程序将其检测为无效。

The logic is simple: 逻辑很简单:
1. Check if the task-list.xml and task-list-schema.xsd exist. 1.检查task-list.xmltask-list-schema.xsd存在。
2. if yes , unmarshall the XML, prepare rows using data from XML document, add rows to table. 2.如果 ,请解组XML,使用XML文档中的数据准备行,然后将行添加到表中。
3. if no , prepare a blank GUI. 3.如果 ,请准备一个空白的GUI。

The problem is that the XML does not conform to the schema. 问题是XML不符合架构。 The problem is not in the generated XML or the schema it is in the classes used for binding . 问题不在生成的XML或架构中,而是在用于绑定的类中 Here is how they are: 它们是这样的:

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

So, FormatList and Task both share the same class Format because each video conversion task has an associated format with it. 因此, FormatListTask都共享同一类Format因为每个视频转换任务都具有关联的格式。

Here is the error I get: 这是我得到的错误:
在此处输入图片说明

Here is the generated XML: 这是生成的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>  

How do I solve this? 我该如何解决?

Classes

@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

}

The error doesn't seem to match the XML you posted. 该错误似乎与您发布的XML不匹配。 That error is saying that JAXB is attempting to unmarshal a format-list element but doesn't know what to do with it. 该错误表示JAXB正在尝试取消format-list元素,但不知道如何处理它。 There's no format-list in that XML. 该XML中没有format-list From the given error, I'd expect that you have code like this: 从给定的错误,我希望您有这样的代码:

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

and you're giving it FormatList XML instead of TaskList XML as input. 并为其提供FormatList XML而不是TaskList XML作为输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM