JAXB解组不起作用

时间:2012-04-03 08:50:24

标签: java jaxb marshalling unmarshalling

我已经设置了一个项目来测试另一个项目的编组。 编组工作正在进行中。我得到了正确的xml文件,但是解组不起作用。我只获取Relation name(String)。缺少属性和功能依赖性。

编辑:这是来源: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>

2 个答案:

答案 0 :(得分: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 :(得分:0)

您需要使用@XmlRootElement为您的课程添加注释。例如。 Attribute没有注释