简体   繁体   中英

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. The generated XML, manually, conforms to the schema however the program detects it as invalid.

The logic is simple:
1. Check if the task-list.xml and task-list-schema.xsd exist.
2. if yes , unmarshall the XML, prepare rows using data from XML document, add rows to table.
3. if no , prepare a blank GUI.

The problem is that the XML does not conform to the schema. The problem is not in the generated XML or the schema it is in the classes used for binding . 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.

Here is the error I get:
在此处输入图片说明

Here is the generated 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. That error is saying that JAXB is attempting to unmarshal a format-list element but doesn't know what to do with it. There's no format-list in that XML. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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