使用XStream读取XML文件

时间:2014-10-15 13:07:51

标签: java xml xstream

我有这个xml:

<testsuite name = "testsuite1" time = "19.0" tests = "2" skipped = "0" errors = "1"  failures = "1">
<testcase name = "test1" time = "10.0">
    <failure type = "testcase" message = "error"></failure>
</testcase>
<testcase name = "test2" time = "9.0">
    <failure type = "testcase" message = "error"></failure>
</testcase>

我定义了3个clases:

TestSuiteBean.class

public class TestSuiteBean {

private List<TestCaseBean> testcases;

@XStreamAsAttribute
private String name;

@XStreamAsAttribute
private long time;

@XStreamAsAttribute
private int failures;

@XStreamAsAttribute
private int tests;

@XStreamAsAttribute
private int skipped;

@XStreamAsAttribute
private int errors;

public String getName() {
    return name;
}

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

public long getTime() {
    return time;
}

public void setTime(long time) {
    this.time = time;
}

public int getFailures() {
    return failures;
}

public void setFailures(int failures) {
    this.failures = failures;
}

public int getTests() {
    return tests;
}

public void setTests(int tests) {
    this.tests = tests;
}

public int getSkipped() {
    return skipped;
}

public void setSkipped(int skipped) {
    this.skipped = skipped;
}

public int getErrors() {
    return errors;
}

public void setErrors(int errors) {
    this.errors = errors;
}

public List<TestCaseBean> getTestcases() {
    if (testcases == null) {
        testcases = new ArrayList<TestCaseBean>();
    }
    return testcases;
}

public void setTestcases(List<TestCaseBean> testcases) {
    this.testcases = testcases;
}

}

TestCaseBean.class

public class TestCaseBean {

private FailureBean failure;

@XStreamAsAttribute
private String name;

@XStreamAsAttribute
private long time;

public String getName() {
    return name;
}

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

public long getTime() {
    return time;
}

public void setTime(long time) {
    this.time = time;
}

public FailureBean getFailure() {
    return failure;
}

public void setFailure(FailureBean failure) {
    this.failure = failure;
}

}

FailureBean.class

public class FailureBean {

private String type;
private String message;

public String getType() {
    return type;
}

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

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

}

和main.class

final XStream xStream = new XStream(new StaxDriver());
    xStream.setMode(XStream.ID_REFERENCES);
    xStream.alias("testsuite", TestSuiteBean.class);
    xStream.alias("testcase", TestCaseBean.class);
    xStream.alias("failure", FailureBean.class);

    FileInputStream file;
    try {
        file = new FileInputStream("config1.xml");
        TestSuiteBean xml = (TestSuiteBean) xStream.fromXML(file);

我得到下一个错误:

线程中的异常&#34; main&#34; com.thoughtworks.xstream.converters.ConversionException:类型为el2.test.zephyr.read.TestCaseBean的元素测试用例未定义为el2.test.zephyr.read.TestSuiteBean类型中的字段 ----调试信息---- class:el2.test.zephyr.read.TestSuiteBean required-type:el2.test.zephyr.read.TestSuiteBean converter-type:com.thoughtworks.xstream.converters.reflection.ReflectionConverter 行号:4

版本:null

1 个答案:

答案 0 :(得分:0)

testcase实际上多次出现,所以我猜您需要替换:

xStream.alias("testsuite", TestSuiteBean.class)

xStream.addImplicitCollection(TestSuiteBean.class, "testcases", "testcase", TestCaseBean.class)