JAXB - 嵌套的自定义对象编组和解组

时间:2018-04-30 22:05:17

标签: java xml jaxb marshalling unmarshalling

我正在尝试通过解组从ReadyAPI解析生成的.xml报告,但是在构建类时遇到了问题。我的例子比我在网上完成的大部分研究要复杂得多,所以我很难对它们进行比较。任何人都可以帮我构建这样的对象结构吗?

Example.xml

<testSuiteResults>
  <testSuite>
    <startTime>00:00:00</startTime>
    <status>PASS</status>
    <testSuiteName>Example Test Suite Name</testSuiteName>
    <timeTaken>246</timeTaken>
    <testRunnerResults>
      <testCase>
        <startTime>00:00:00</startTime>
        <status>PASS</status>
        <testCaseId>111aaa111aaa111aaa111</testCaseId>
        <testCaseName>Example TestCase Name</testCaseName>
        <timeTaken>123</timeTaken>
        <testStepResults>
          <result>
            <message>Example Message</message>
            <name>Example Result Name</name>
            <order>1</order>
            <started>00:00:00</started>
            <status>PASS</status>
            <timeTaken>123</timeTaken>
          </result>
        </testStepResults>
        <testStepParameters>
          <parameters>
            <iconPath>/icon_path.png</iconPath>
            <testStepName>Example Test</testStepName>
          </parameters>
        </testStepParameters>
        </failedTestSteps>
      </testCase>
      <testCase>
        <reason>Example Fail Reason</reason>
        <startTime>00:00:00</startTime>
        <status>FAIL</status>
        <testCaseId>123abc123abc123abc123</testCaseId>
        <testCaseName>Example Test Case Name 2</testCaseName>
        <timeTaken>123</timeTaken>
        <testStepResults>
          <result>
            <message>Example Message 2</message>
            <name>Example Test Step Name 2</name>
            <order>1</order>
            <started>00:00:00</started>
            <status>FAIL</status>
            <timeTaken>123</timeTaken>
          </result>
        </testStepResults>
        <testStepParameters>
          <parameters>
            <iconPath>/icon_path_2.png</iconPath>
            <testStepName>Example Test Step Name 2</testStepName>
          </parameters>
        </testStepParameters>
        <failedTestSteps>
          <error>
            <detail>Example Detail</detail>
            <icon>icon.png</icon>
            <testCaseName>Example Test Case Name 2</testCaseName>
            <testStepName>Example Test Step Name 2</testStepName>
            <testSuiteName>Example Test Suite Name</testSuiteName>
          </error>
        </failedTestSteps>
      </testCase>
    </testRunnerResults>
  </testSuite>
</testSuiteResults>

经过多次迭代后,我已经登陆了这个结构:

@XmlRootElement(name = "testSuiteResults")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestSuiteResults {
    @XmlElement(name = "testSuite")
    private List<TestSuite> testSuites;

    public void setTestSuites(List<TestSuite> testSuites) {
        this.testSuites = testSuites;
    }

    public List<TestSuite> getTestSuites() {
        return this.testSuites;
    }

    public boolean hasTestSuites() {
        return this.testSuites != null && this.testSuites.size() > 0;
    }
}

@XmlRootElement(name = "testSuite")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestSuite {
    @XmlElement(name = "startTime")
    private String startTime;
    @XmlElement(name = "status")
    private String status;
    @XmlElement(name = "testSuiteName")
    private String testSuiteName;
    @XmlElement(name = "timeTaken")
    private String timeTaken;
    @XmlElementWrapper(name = "testRunnerResults")
    @XmlElement(name = "testCase", type = TestCase.class)
    private List<TestCase> testRunnerResults;

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setTestSuiteName(String testSuiteName) {
        this.testSuiteName = testSuiteName;
    }

    public void setTimeTaken(String timeTaken) {
        this.timeTaken = timeTaken;
    }

    public void setTestRunnerResults(List<TestCase> testRunnerResults) {
        this.testRunnerResults = testRunnerResults;
    }

    public String getTestSuitename() {
        return this.testSuiteName;
    }

    public List<TestCase> getTestCases() {
        return this.testRunnerResults;
    }

    public boolean hasTestCases() {
        return this.testRunnerResults != null;
    }
}

@XmlRootElement(name = "testCase")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestCase {
    @XmlElement(name = "reason")
    private String reason;
    @XmlElement(name = "startTime")
    private String startTime;
    @XmlElement(name = "status")
    private String status;
    @XmlElement(name = "testCaseId")
    private String testCaseId;
    @XmlElement(name = "testCaseName")
    private String testCaseName;
    @XmlElement(name = "timeTaken")
    private String timeTaken;
    @XmlElementWrapper(name = "testStepResults")
    @XmlElement(name = "result")
    private List<TestStepResult> testStepResults;
    @XmlElementWrapper(name = "testStepParameters")
    @XmlElement(name = "parameters")
    private List<TestStepParameter> testStepParameters;
    @XmlElementWrapper(name = "failedTestSteps")
    @XmlElement(name = "error")
    private List<TestStepError> failedTestSteps;

    public void setReason(String reason) {
        this.reason = reason;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setTestCaseId(String testCaseId) {
        this.testCaseId = testCaseId;
    }

    public void setTestCaseName(String testCaseName) {
        this.testCaseName = testCaseName;
    }

    public void setTimeTaken(String timeTaken) {
        this.timeTaken = timeTaken;
    }

    public void setTestStepResults(List<TestStepResult> testStepResults) {
        this.testStepResults = testStepResults;
    }

    public void setTestStepParameters(List<TestStepParameter> testStepParameters) {
        this.testStepParameters = testStepParameters;
    }

    public void setFailedTestSteps(List<TestStepError> failedTestSteps) {
        this.failedTestSteps = failedTestSteps;
    }

    public String getReason() {
        return this.reason;
    }

    public String getStartTime() {
        return this.startTime;
    }

    public String getStatus() {
        return this.status;
    }

    public String getTestCaseId() {
        return this.testCaseId;
    }

    public String getTestCaseName() {
        return this.testCaseName;
    }

    public String getTimeTaken() {
        return this.timeTaken;
    }

    public List<TestStepResult> getTestStepResults() {
        return this.testStepResults;
    }

    public List<TestStepParameter> getTestStepParameters() {
        return this.testStepParameters;
    }

    public List<TestStepError> getFailedTestSteps() {
        return this.failedTestSteps;
    }
}

@XmlRootElement(name = "result")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestStepResult {
    @XmlElement(name = "message")
    private String message;
    @XmlElement(name = "name")
    private String testStepName;
    @XmlElement(name = "order")
    private int order;
    @XmlElement(name = "started")
    private String started;
    @XmlElement(name = "status")
    private String status;
    @XmlElement(name = "timeTaken")
    private String timeTaken;

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

    public void setTestStepName(String testStepName) {
        this.testStepName = testStepName;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public void setStarted(String started) {
        this.started = started;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setTimeTaken(String timeTaken) {
        this.timeTaken = timeTaken;
    }

    public String getTestStepName() {
        return this.testStepName;
    }
}

@XmlRootElement(name = "paramters")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestStepParameter {
    @XmlElement(name = "iconPath")
    private String iconPath;
    @XmlElement(name = "testStepName")
    private String testStepName;

    public void setIconPath(String iconPath) {
        this.iconPath = iconPath;
    }

    public void setTestStepName(String testStepName) {
        this.testStepName = testStepName;
    }

    public String getTestStepName() {
        return this.testStepName;
    }
}

@XmlRootElement(name = "error")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestStepError {
    @XmlElement(name = "detail")
    private String detail;
    @XmlElement(name = "icon")
    private String icon;
    @XmlElement(name = "testCaseName")
    private String testCaseName;
    @XmlElement(name = "testStepName")
    private String testStepName;
    @XmlElement(name = "testSuiteName")
    private String testSuiteName;

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public void setTestCaseName(String testCaseName) {
        this.testCaseName = testCaseName;
    }

    public void setTestStepName(String testStepName) {
        this.testStepName = testStepName;
    }

    public void setTestSuiteName(String testSuiteName) {
        this.testSuiteName = testSuiteName;
    }
}

Example.xml是报告的生成方式,我已经开始尝试使用示例数据自行编组,以确认我正在构建此报告。我已经得到了这个回复:

<testSuiteResults>
    <testSuite>
        <startTime>00:00:00</startTime>
        <status>PASS</status>
        <testSuiteName>TestSuiteName</testSuiteName>
        <timeTaken>123</timeTaken>
        <testRunnerResults/>
    </testSuite>
</testSuiteResults>

它总是在填充testRunnerResults时停止,所以我试图研究XmlAdapters,但是我在理解如何将它用于这个结构时遇到了很多麻烦。

0 个答案:

没有答案