JAXB嵌套XML-具有属性的List <object>相同名称:无法显示XML数据

时间:2018-11-16 02:27:20

标签: xml jaxb

我有一个看起来像这样的xml

<?xml version="1.0" encoding="UTF-8"?>
<TestCases>
  <TestCase id="TC04">
    <statement sequence="1">This is TC04, statement 1</statement>
    <statement sequence="2">This is TC04, statement 2</statement>
    <statement sequence="3">This is TC04, statement 3</statement>
    <statement sequence="4">This is TC04, statement 4</statement>
  </TestCase>
  <TestCase id="TC05">
    <statement sequence="1">This is TC05, statement 1</statement>
    <statement sequence="2">This is TC05, statement 2</statement>
    <statement sequence="3">This is TC05, statement 3</statement>
    <statement sequence="4">This is TC05, statement 4</statement>
  </TestCase>
</TestCases>

到目前为止,我已经能够在TestCases下获得所有TestCase并显示序列号。

TC04 1 = null,2 = null,3 = null,4 = null TC05 1 = null,2 = null,3 = null,4 = null

但是,当显示值时,我得到的是空值。我认为这需要一个XMLAdapter,但我并不熟悉,也无法理解如何创建一个读取/存储语句节点的属性和xml数据的适配器。

下面是我的Java代码。

package Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "TestCases")
public class TestCases {

    @XmlElement(name = "TestCase")
    private List<TestCase> testcases;

    public List<TestCase> getTestCases() {
        if (this.testcases == null)
            this.testcases = new ArrayList<TestCase>();
        return testcases;
    }

    @XmlRootElement(name = "TestCase")
    public static class TestCase {

        @XmlAttribute(name = "id")
        private String id;

        public String getId() {
            return this.id;
        }

        @XmlElement(name = "statement")
        public List<Statement> statements;

        public List<Statement> getStatements() {
            if (this.statements == null)
                this.statements = new ArrayList<Statement>();
            return statements;
        }

        @XmlRootElement(name = "statement")
        public static class Statement {
            private String name;

            @XmlAttribute(name = "sequence")
            private String sequence;

            public String getSequence() {
                return this.sequence;
            }

            public String getStatement() {
                return this.name;
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

  1. 您应仅在@XmlRootElement上使用TestCases
  2. 您可能还需要在@XmlValue上使用name,然后添加 @XmlAccessorType(XmlAccessType.FIELD)在您的课程中。 This post可能会有所帮助。

    @XmlRootElement(name = "TestCases")
    public class TestCases {
    
        @XmlElement(name = "TestCase")
        private List<TestCase> testcases;
    
        public List<TestCase> getTestCases() {
            if (this.testcases == null)
                this.testcases = new ArrayList<TestCase>();
            return testcases;
        }
    
        public static class TestCase {
    
            @XmlAttribute(name = "id")
            private String id;
    
            public String getId() {
                return this.id;
            }
    
            @XmlElement(name = "statement")
            public List<Statement> statements;
    
            public List<Statement> getStatements() {
                if (this.statements == null)
                    this.statements = new ArrayList<Statement>();
                return statements;
            }
    
            @XmlAccessorType(XmlAccessType.FIELD)
            public static class Statement {
                @XmlValue
                private String name;
    
                @XmlAttribute(name = "sequence")
                private String sequence;
    
                public String getSequence() {
                    return this.sequence;
                }
    
                public String getStatement() {
                    return this.name;
                }
            }
    
        }
    }