Java JAXB 生成的 xml 中的“xsi:type”和“xmlns:xsi”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20679163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:33:14  来源:igfitidea点击:

"xsi:type" and "xmlns:xsi" in generated xml by JAXB

javaxmljaxb

提问by psmagin

I use JAXB to create folder and files hierarchy

我使用 JAXB 创建文件夹和文件层次结构

My model:

我的型号:

@XmlRootElement
public class Root {

    @XmlAttribute
    private String path;

    @XmlElement(name = "dir")
    private ArrayList<Dir> rootContentDirs = null;

    @XmlElement(name = "file")
    private ArrayList<FileObj> rootContentFiles = null;


    public void setRootContentDirs(ArrayList<Dir> rootContentDirs) {
        this.rootContentDirs = rootContentDirs;
    }

    public void setRootContentFiles(ArrayList<FileObj> rootContentFiles) {
        this.rootContentFiles = rootContentFiles;
    }

    public void setPath(String path) {
        this.path = path;
    }
 }

public class Dir {

    @XmlAttribute
    private String name;

    @XmlElement(name = "dir")
    private ArrayList dirs = null;

    @XmlElement(name = "file")
    private ArrayList files = null;

    public void setName(String name) {
        this.name = name;
    }

    public void setDirs(ArrayList dirs) {
        this.dirs = dirs;
    }

    public void setFiles(ArrayList files) {
        this.files = files;
    }
 }

public class FileObj{

   @XmlAttribute
   private String name;
   @XmlAttribute
   private long size;
   @XmlAttribute
   private String type;


   public void setName(String name) {
       this.name = name;
   }

   public void setSize(long size) {
       this.size = size;
   }

   public void setType(String type) {
       this.type = type;
   }
}

I want to make tree of dirs and files:

我想制作目录和文件树:

public class XmlByJaxb extends Generator {

private static final String JAXB_XML = "./jaxb.xml";
private static Root root = null;

@Override
public void output() throws IOException {
    JAXBContext context = null;
    Marshaller m = null;
    try {
        context = JAXBContext.newInstance(Root.class, Dir.class, FileObj.class);
        m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(root, new File(JAXB_XML));
    } catch (JAXBException e) {

    }
}

@Override
public void run() {
    ArrayList<FileObj> rootFiles = addFiles(dir);
    ArrayList<Dir> rootDirs = make(dir);

    root = new Root();
    root.setPath(dir.getPath());
    root.setRootContentFiles(rootFiles);
    root.setRootContentDirs(rootDirs);
/.../
}

But I hava strange "xsi:type" and "xmlns:xsi" in generated xml:

但我在生成的 xml 中有奇怪的“xsi:type”和“xmlns:xsi”:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root path="/home/phlamey/IdeaProjects/FileUtillite">
<dir name="src">
    <dir xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="dir" name="test">
        <dir xsi:type="dir" name="java">
            /.../ 

So my question: what does this mean and how does remove this?

所以我的问题是:这是什么意思以及如何删除它?

采纳答案by bdoughan

In your Dirclass you are not specifying the type of your collection this is why JAXB is adding the xsi:typeattributes.

在您的Dir类中,您没有指定集合的​​类型,这就是 JAXB 添加xsi:type属性的原因。

You have:

你有:

@XmlElement(name = "dir")
private ArrayList dirs;

If your ArrayListis going to contain instances of Dirthen you can do:

如果您ArrayList要包含实例,Dir那么您可以执行以下操作:

@XmlElement(name = "dir")
private ArrayList<Dir> dirs = null;

If for some reason you don't want to specify the type on the collection then you can do it in the @XmlElementannotation:

如果由于某种原因您不想在集合上指定类型,那么您可以在@XmlElement注释中进行:

@XmlElement(name = "dir", type=Dir.class)
private ArrayList dirs = null;