2012-04-03 75 views
1

我已經設置了一個項目來測試另一個項目的編組。編組正在工作。我得到正確的xml文件,但解組不起作用。我只獲得關係名稱(字符串)。缺少屬性和功能依賴關係。JAXB解組無法正常工作

編輯:這裏是源:Sourcecode

請看一看類:

主營:

public class Main { 

public static void main(String[] args){ 

    Relation db = new Relation(); 

    Attribute a1 = new Attribute("Attribute 1", true, false); 
    Attribute a2 = new Attribute("Attribute 2", false, false); 
    Attribute a3 = new Attribute("Attribute 3", false, true); 

    db.addAttribute(a1); 
    db.addAttribute(a2); 
    db.addAttribute(a3); 

    ArrayList<String> src = new ArrayList<String>(); 
    src.add("Attribute 1"); 

    ArrayList<String> dest = new ArrayList<>(); 
    dest.add("Attribute 2,Attribute 3"); 

    FDs f1 = new FDs(src, dest); 

    db.addFd(f1); 

    exportToXml saver = new exportToXml(); 
    try { 
     saver.SaveDbNow(db); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    //Export again to test output 
    Relation db2 = new Relation(); 

    importFromXml reader = new importFromXml(); 
    try { 
     reader.ReadDbNow(db2); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     saver.SaveDbNow2(db2); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

} 

關係:

@XmlRootElement(name = "Relation") 
public class Relation { 
@XmlElement(name = "RelName") 
String name; 

@XmlElement(name = "Attribute") 
private ArrayList<Attribute> attrList; 
@XmlElement(name = "FD") 
private ArrayList<FDs> fdsList; 

public Relation() { 
    this.attrList = new ArrayList<>(); 
    this.fdsList = new ArrayList<>(); 
    this.name = "Testname"; 
} 

public Relation(String name, ArrayList<Attribute> attrList, ArrayList<FDs> fdsList) { 
    super(); 
    this.attrList = attrList; 
    this.fdsList = fdsList; 
} 

@XmlTransient 
public ArrayList<Attribute> getAttrList() { 
    return attrList; 
} 

public void setAttrList(ArrayList<Attribute> attrList) { 
    this.attrList = attrList; 
} 

@XmlTransient 
public ArrayList<FDs> getFdsList() { 
    return fdsList; 
} 

public void setFdsList(ArrayList<FDs> fdsList) { 
    this.fdsList = fdsList; 
} 

public void addAttribute(Attribute a) { 
    this.attrList.add(a); 
} 

public void addFd(FDs fd) { 
    this.fdsList.add(fd); 
} 

} 

屬性:

public class Attribute { 
@XmlElement(name = "Attributename") 
private String name; 
@XmlElement(name = "isPK") 
private boolean isPK; 
@XmlElement(name = "isFK") 
private boolean isFK; 

public Attribute(){ 

} 


public Attribute(String name, boolean isPK, boolean isFK) { 
    super(); 
    this.name = name; 
    this.isPK = isPK; 
    this.isFK = isFK; 
} 

@XmlTransient 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
@XmlTransient 
public boolean isPK() { 
    return isPK; 
} 

public void setPK(boolean isPK) { 
    this.isPK = isPK; 
} 

@XmlTransient 
public boolean isFK() { 
    return isFK; 
} 
public void setFK(boolean isFK) { 
    this.isFK = isFK; 
} 
} 

FD:

public class FDs { 
@XmlElement(name = "Source") 
private ArrayList<String> src; 
@XmlElement(name = "Destination") 
private ArrayList<String> dest; 

public FDs(){ 

} 

public FDs(ArrayList<String> src, ArrayList<String> dest) { 
    super(); 
    this.src = src; 
    this.dest = dest; 
} 

@XmlTransient 
public ArrayList<String> getSrc() { 
    return src; 
} 
public void setSrc(ArrayList<String> src) { 
    this.src = src; 
} 

@XmlTransient 
public ArrayList<String> getDest() { 
    return dest; 
} 
public void setDest(ArrayList<String> dest) { 
    this.dest = dest; 
} 
} 

出口:

public class exportToXml { 

public void SaveDbNow(Object saveMe) throws Exception { 
    JAXB.marshal(saveMe, new File("test.xml")); 
} 

public void SaveDbNow2(Object saveMe) throws Exception { 
    JAXB.marshal(saveMe, new File("test2.xml")); 
} 
} 

導入:

public class importFromXml { 

public void ReadDbNow(Object readMe) throws Exception { 
    readMe = JAXB.unmarshal(new FileInputStream("test.xml"), Relation.class); 
} 
} 

提前感謝!

編輯: 輸出1:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Relation> 
<RelName>Testname</RelName> 
<Attribute> 
    <Attributename>Attribute 1</Attributename> 
    <isPK>true</isPK> 
    <isFK>false</isFK> 
</Attribute> 
<Attribute> 
    <Attributename>Attribute 2</Attributename> 
    <isPK>false</isPK> 
    <isFK>false</isFK> 
</Attribute> 
<Attribute> 
    <Attributename>Attribute 3</Attributename> 
    <isPK>false</isPK> 
    <isFK>true</isFK> 
</Attribute> 
<FD> 
    <Source>Attribute 1</Source> 
    <Destination>Attribute 2,Attribute 3</Destination> 
</FD> 

輸出2:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Relation> 
<RelName>Testname</RelName> 
</Relation> 
+1

你混合getter和領域。使用例如'@XmlAccessorType(XmlAccessType.FIELD)'類的註解來決定一個訪問器類型。 – 2012-04-03 09:15:44

+1

下面的文章將有助於@GrzegorzGrzybek給出的建議:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html – 2012-04-03 10:17:03

+0

感謝@GrzegorzGrzybek和@BlaiseDoughan!我會看看它 – 2012-04-03 12:43:04

回答

1

解釋當前的行爲

Main類實例化的Relation一個新的實例構造填充name屬性"Testname"。在Relation的實例中沒有其他任何內容被填充,這就是爲什麼你看到了你的XML輸出。

// Export again to test output 
    Relation db2 = new Relation(); 

    importFromXml reader = new importFromXml(); 
    try { 
     reader.ReadDbNow(db2); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     saver.SaveDbNow2(db2); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

潛在的解決方案

更改ReadDbNow方法上importFromXml類返回它解組的Relation實例。

import java.io.FileInputStream; 
import javax.xml.bind.JAXB; 

public class importFromXml { 

    public Relation ReadDbNow() throws Exception { 
     return JAXB 
       .unmarshal(new FileInputStream("test.xml"), Relation.class); 
    } 
} 

Main類更改您的代碼執行以下操作:

// Export again to test output 
    Relation db2 = null; 

    importFromXml reader = new importFromXml(); 
    try { 
     db2 = reader.ReadDbNow(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     saver.SaveDbNow2(db2); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+1

非常感謝!我認爲這個錯誤是由Arraylists。我花了很多時間在錯誤的結尾尋找錯誤! – 2012-04-11 10:56:35

0

您需要@XmlRootElement來註釋類。例如。 Attribute沒有註釋

+0

對不起,沒有工作。更新第1篇文章。添加了2個xml文件 – 2012-04-03 09:05:10

+0

你對每個類的proeprty都有setter方法嗎? – fmucar 2012-04-03 09:10:03