"的xsi:type"和“xmlns:xsi”;在JAXB生成的xml中 [英] "xsi:type" and "xmlns:xsi" in generated xml by JAXB

查看:172
本文介绍了"的xsi:type"和“xmlns:xsi”;在JAXB生成的xml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use JAXB to create folder and files hierarchy

我的模型:

@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;
   }
}

我想制作dirs和文件树:

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);
/.../
}

但我很奇怪xsi:在生成的xml中键入和xmlns:xsi:

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

<?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?

推荐答案

在你的 Dir 类中,你没有指定类型你的收藏这就是JAXB添加 xsi:type 属性的原因。

In your Dir class you are not specifying the type of your collection this is why JAXB is adding the xsi:type attributes.

你有:

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

如果您的 ArrayList 将包含实例 Dir 然后你可以这样做:

If your ArrayList is going to contain instances of Dir then you can do:

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

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

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

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

这篇关于&QUOT;的xsi:type&QUOT;和“xmlns:xsi”;在JAXB生成的xml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆