如何通过jaxbjavapojo获取子xml节点的属性和值
问题:-在此xml片段中如何通过jaxbjavapojo获取子xml节点的属性和值,java,xml-parsing,jaxb,maven-jaxb2-plugin,Java,Xml Parsing,Jaxb,Maven Jaxb2 Plugin,问题:-在此xml片段中 <testcase classname="testcase1" name="method1" time="40.059"> <error message="timeout" type="org.openqa.selenium.TimeoutException"> <![CDATA[org.openqa.seleni
<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>
但是如果我把它改成
私密字符串审裁处
然后使用getError()可以访问error节点的值。(
当我需要访问所有(即消息、错误节点的类型和值)时,我需要做什么
我试过了
@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>
错误类
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;
}
}
<?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>
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;
}