在具有JAXB的继承结构的xml之后进行解组 [英] Unmarshalling following xml having inheritance structure with JAXB
问题描述
我想要解组这个xml
I want to unmarshal this xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<command>dir</command>
<directory name="folder">
<file>
<lastModified>2016-06-06 12:45 AM</lastModified>
<name>input.txt</name>
<size>123</size>
<type>file</type>
</file>
<file>
<lastModified>2016-06-06 12:45 AM</lastModified>
<name>data.txt</name>
<size></size>
<type>directory</type>
</file>
</directory>
</response>
这是我的班级结构
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement
@XmlSeeAlso({MessageResponse.class})
class Response {
String command;
public Response() {
}
@XmlElement
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
}
@XmlRootElement
class MessageResponse extends Response{
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class DirListingResponse extends Response{
String name;
Directory directory;
@XmlElement
public Directory getDirectory() {
return directory;
}
public void setDirectory(Directory directory) {
this.directory = directory;
}
}
class Directory {
ArrayList<File> file;
String name;
public Directory() {
}
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public ArrayList<File> getFile() {
return file;
}
public void setFile(ArrayList<File> file) {
this.file = file;
}
}
class File {
String name, type;
String lastModified;
long size;
public File() {
}
public File(String name, String type, String lastModified, long size) {
this.name = name;
this.type = type;
this.lastModified = lastModified;
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLastModified() {
return lastModified;
}
public void setLastModified(String lastModified) {
this.lastModified = lastModified;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
}
和主要班级
try {
jaxbContext = JAXBContext.newInstance(Response.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Object obj = jaxbUnmarshaller.unmarshal(new InputSource(new StringReader(inputXml)));
} catch (JAXBException e) {
e.printStackTrace();
}
但这不起作用。由于没有正确的注释,obj仅包含响应类的命令字段。在响应标签下,directory和message标签基于触发的命令出现。如果命令是dir,则directory标记否则为message标记。所以我想要这个unmarshal并将结果保存在相对继承的类中。我该如何解决这个问题?
but this is not working accordingly. obj contains only command field of response class due to not the proper annotation. Under "response" tag "directory" and "message" tag appears based on command fired. If the command is "dir" then "directory" tag otherwise "message" tag. So I want this unmarshal and save the result in relative inherited class. How can I solve this ?
推荐答案
我有一个使用Sax-Parser的解决方案。我将其上传到 Github 。应该有更好的解决方案(包括隐式打字)。但这可以解决您的问题,同时创建新问题。对文件和目录等的更改会在相应的处理程序中引入更改...
I have a solution which uses the Sax-Parser. I uploaded it to Github. There should be better solutions (including implicit typings). But this would solve your problems, while creating new ones. Changes to File and Directory, etc. would introduce changes in the corresponding handlers...
这篇关于在具有JAXB的继承结构的xml之后进行解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!