【问题标题】:Read a XML file with XStream使用 XStream 读取 XML 文件
【发布时间】:2014-10-15 13:07:50
【问题描述】:

我有这个 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 个类别:

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);

我得到下一个错误:

线程“main”com.thoughtworks.xstream.converters.ConversionException 中的异常:el2.test.zephyr.read.TestCaseBean 类型的元素测试用例未定义为 el2.test.zephyr.read.TestSuiteBean 类型中的字段 ---- 调试信息 ---- 类:el2.test.zephyr.read.TestSuiteBean 所需类型:el2.test.zephyr.read.TestSuiteBean 转换器类型:com.thoughtworks.xstream.converters.reflection.ReflectionConverter 行号:4

版本:空

【问题讨论】:

    标签: java xml xstream


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多