prix
prix

Reputation: 143

Unmarshaling XML

I'm trying to unmarshal existing xml file so that i can handle the data properly but the XML structure looks weird. below is the sample of the xml together with the objects I've created.

<?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>

Below are my java classes. I just removed the variables for readability.

@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;
    }
}

I was trying to get all the records within the ListOfSections tag but I'm only getting the last section which is AA1002.

for the ListOfStudents I'm only getting the first record in my case I'm only getting record with sequence number 15

<SequenceNumber>12</SequenceNumber>
<RecordCount>2</RecordCount>
<StudentId>201614355</StudentId>

Upvotes: 0

Views: 68

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136102

This simplified code works with your xml, it should give you a hint where to look for the problem:

class Record {
    @XmlElementWrapper(name="ListOfSections")
    @XmlElement(name="SECTION")
    List<String> listOfSections;
}

here how to run:

    Record r = JAXB.unmarshal(new File("1.xml"), Record.class);
    System.out.println(r.listOfSections);

result:

[AA1001, AA1002]

Upvotes: 1

Related Questions