如何通过jaxb java pojo

问题描述

问题:-在此xml代码段中,

  <testcase classname="testcase1" name="method1" time="40.059">
    <error message="timeout" type="org.openqa.selenium.TimeoutException">
        <![CDATA[org.openqa.selenium.TimeoutException: timeout: Timed out receiving message]]>
    </error>
</testcase> 

在Testcase类中,如果我将错误节点转换为Error类,那么我只能通过getMessage()和getType()访问消息并键入属性。

@XmlElement(name = "error")
private Error sError;

但是,如果我将其更改为 私有字符串sError;

并使用getError()然后可以访问错误节点的值。 (

我需要访问所有内容(即消息,错误节点的类型和值)时需要做什么?

我尝试过

 @XmlElement(name = "error")
 private Error sError;
 private String error;

它不接受针对一个元素节点的两种getter方法。

我正在使用这些库从pom.xml中的xml中读取

<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-core</artifactId>
  <version>2.3.0.1</version>
</dependency>
<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.1</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.3.1</version>
</dependency>
<dependency>
  <groupId>org.javassist</groupId>
  <artifactId>javassist</artifactId>
  <version>3.25.0-GA</version>
</dependency>

我的xml

<?xml version="1.0" encoding="UTF-8"?>

<testsuite hostname="abc" failures="0" tests="1" name="suite1" time="40.059" errors="1" timestamp="20 Sep 2020 20:16:36 GMT">
<testcase classname="testcase1" name="method1" time="40.059">
    <error message="timeout" type="org.openqa.selenium.TimeoutException">
        <![CDATA[org.openqa.selenium.TimeoutException: timeout: Timed out receiving message]]>
    </error>
</testcase> 
</testsuite> 

我的TestSuite类

import javax.xml.bind.annotation.*;
import java.util.List;

@XmlRootElement(name = "testsuite")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestSuite {

@XmlAttribute(name = "hostname")
private String hostname;
@XmlAttribute(name = "failures")
private String failures;
@XmlAttribute(name = "tests")
private String tests;
@XmlAttribute(name = "name")
private String name;
@XmlAttribute(name = "time")
private String time;
@XmlAttribute(name = "errors")
private String errors;
@XmlAttribute(name = "timestamp")
private String timestamp;
@XmlAttribute(name = "skipped")
private String skipped;
@XmlElement(name = "testcase")
private List<TestCase> testcases;

public TestSuite() {
    super();

}

public TestSuite(String hostname,String failures,String tests,String name,String time,String errors,String timestamp,String skipped,List<TestCase> testCases) {
    super();
    this.hostname = hostname;
    this.failures = failures;
    this.tests = tests;
    this.name = name;
    this.time = time;
    this.errors = errors;
    this.timestamp = timestamp;
    this.skipped = skipped;
    this.testcases = testCases;
}

public String getHostname() {
    return hostname;
}

public void setHostname(String hostname) {
    this.hostname = hostname;
}

public String getFailures() {
    return failures;
}

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

public String getTests() {
    return tests;
}

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

public String getName() {
    return name;
}

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

public String getTime() {
    return time;
}

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

public String getErrors() {
    return errors;
}

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

public String getTimestamp() {
    return timestamp;
}

public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}

public String getSkipped() {
    return skipped;
}

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

public List<TestCase> getTestcases() {
    return testcases;
}

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

@Override
public String toString() {
    return "TestSuite [hostname=" + hostname +
            ",failures=" + failures +
            ",tests=" + tests +
            ",name=" + name +
            ",time=" + time +
            ",errors=" + errors +
            ",timestamp=" + timestamp +
            ",skipped=" + skipped +
            ",testcases=" + testcases +
            "]";
}

}

测试用例类

@XmlRootElement(name = "testcase")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class TestCase {

@XmlAttribute(name = "classname")
private String mClassname;
@XmlAttribute(name = "name")
private String mName;
@XmlAttribute(name = "time")
private String mTime;
@XmlElement(name = "failure")
private Failure mFailure;
@XmlElement(name = "error")
private String mError;
@XmlElement(name = "error")
private Error sError;

public TestCase() {
    super();
}

public TestCase(String mClassname,String mName,String mTime,Failure mFailure,String mError,Error sError) {
    super();
    this.mClassname = mClassname;
    this.mName = mName;
    this.mTime = mTime;
    this.mFailure = mFailure;
    this.mError = mError;
    this.sError = sError;
}
public Error getsError() {
    return sError;
}

public void setsError(Error sError) {
    this.sError = sError;
}

public String getError() {
    return mError;
}

public void setError(String error) {
    this.mError = error;
}


public Failure getFailure() {
    return mFailure;
}

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

public String getClassname() {
    return mClassname;
}

public void setClassname(String classname) {
    this.mClassname = classname;
}

public String getName() {
    return mName;
}

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

public String getTime() {
    return mTime;
}

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

@Override
public String toString() {
    return "TestCase [name=" + mName + ",classname=" + mClassname + ",time=" + mTime;
}
}

错误类别

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "error")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Error {

@XmlAttribute(name = "message")
private String eMessage;
@XmlAttribute(name = "type")
private String eType;

public Error() {
    super();
}

public Error(String eMessage,String eType) {
    super();
    this.eMessage = eMessage;
    this.eType = eType;
}
public String getMessage() {
    return eMessage;
}

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

public String getType() {
    return eType;
}

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

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)