2013-12-19 58 views
8

我使用JAXB創建的文件夾和文件層次「XSI:類型」和「的xmlns:XSI」在生成的XML由JAXB

我的模型:

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

我要讓迪爾斯的樹文件:

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

但我HAVA奇怪的生成XML 「XSI:類型」 和 「XSI的xmlns」:

<?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"> 
      /.../ 

所以我的問題:這是什麼意思,如何消除這個?

回答

9

在您的Dir類中,您沒有指定集合的​​類型,這就是JAXB添加xsi:type屬性的原因。

您有:

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

如果您ArrayList是要包含Dir情況下,那麼你可以做:

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

如果由於某種原因,你不想對指定類型那麼你可以在@XmlElement註釋中做到這一點:

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

Tha你好。有用。 –

相關問題