我正在尝试解组现有的xml文件,以便我可以正确处理数据,但XML结构看起来很奇怪。下面是xml的示例以及我创建的对象。
<?xml version="1.0" encoding="UTF-8"?>
<Record>
<identifier>11</identifier>
<TotalRecords>266</TotalRecords>
<ListOfSections>
<SECTION>AA1001</SECTION>
<ListOfStudents>
<SequenceNumber>11</SequenceNumber>
<RecordCount>1</RecordCount>
<StudentId>201614354</StudentId>
</ListOfStudents>
<SECTION>AA1002</SECTION>
<ListOfStudents>
<SequenceNumber>15</SequenceNumber>
<RecordCount>1</RecordCount>
<StudentId>201614356</StudentId>
<SequenceNumber>16</SequenceNumber>
<RecordCount>2</RecordCount>
<StudentId>201614355</StudentId>
</ListOfStudents>
</ListOfSections>
</Record>
以下是我的java课程。我刚刚删除了变量以便于阅读。
@XmlRootElement
public class Record {
@XmlElement(name = "TotalRecords")
public Integer getTotalNoOfRecords() {
return totalNoOfRecords;
}
@XmlElement(name = "ListOfSections")
public List<Section> getSectionList() {
return sectionList;
}
}
@XmlRootElement
public class Section {
@XmlElement(name = "SECTION")
public String getSection() {
return section;
}
@XmlElement(name = "ListOfStudents")
public List<Student> getStudentList() {
return studentList;
}
}
@XmlRootElement
public class Student {
@XmlElement(name = "SequenceNumber")
public Integer getSequenceNumber() {
return sequenceNumber;
}
@XmlElement(name = "RecordCount")
public Integer getRecordCount() {
return recordCount;
}
@XmlElement(name = "StudentId")
public Integer getStudentId() {
return studentId;
}
}
我试图获取ListOfSections标签中的所有记录,但我只得到AA1002的最后一部分。
对于ListOfStudents的我只获得了我的第一条记录我只获得序列号为15的记录
<SequenceNumber>12</SequenceNumber>
<RecordCount>2</RecordCount>
<StudentId>201614355</StudentId>
答案 0 :(得分:1)
这个简化的代码适用于你的xml,它应该给你一个提示,在哪里寻找问题:
class Record {
@XmlElementWrapper(name="ListOfSections")
@XmlElement(name="SECTION")
List<String> listOfSections;
}
这里如何运行:
Record r = JAXB.unmarshal(new File("1.xml"), Record.class);
System.out.println(r.listOfSections);
结果:
[AA1001, AA1002]