JAXB List as top level XML
<p><strong>UPDATE:</strong> Ultimately I tried everything suggested here and seemed to have some progress but my API was over engineered for the intended purpose and I ran out of time, so I switched to hard coding "student-list" in the string response of my GET /student call which was more than enough for the proof of concept I needed it for.</p>

<p>I am working on a simple REST service that uses XML to work with student records.</p>

<p>when I do a GET on /student I get:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;students&gt;
    &lt;student&gt;
        &lt;grade&gt;B&lt;/grade&gt;
        &lt;name&gt;Jane&lt;/name&gt;
    &lt;/student&gt;
    ...
&lt;/students&gt;
</code></pre>

<p>However, I would like the output to be:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;student-list&gt;
    &lt;student&gt;
        &lt;grade&gt;B&lt;/grade&gt;
        &lt;name&gt;Jane&lt;/name&gt;
    &lt;/student&gt;
    ...
&lt;/student-list&gt;
</code></pre>

<p>Here is my <strong>Student.java</strong> snippet:</p>

<pre><code>@XmlRootElement(name = "student")
public class Student {
    private String name;
    private String grade;

    public Student() {
    }

    public Student(String name, String grade) {
        super();
        this.name = name;
        this.grade = grade;
    }

    // getters/setters ...

}
</code></pre>

<p>I have tried change the XmlRootElement to be "student-list", but that makes every sub element of the list a student-list type, and ignores the top level element list.</p>

<p>The project uses JAX-RS with a StudentsResource class which I have tried setting the XmlRootElement name on that resource class, but that did not affect any output.</p>

<p>It seems to be automatically naming the top level list based on the StudentsResource class, and I cannot figure out how to override this name.</p>

<p>I have been combing the documentation on this but Java is not my strongest language these days so I am having trouble understanding how the pieces fit together for JAXB and JAX-RS.</p>

<p>Any ideas, or a push in the right direction ?</p>

<p><strong>EDIT: added StudentsResource.java snippet:</strong></p>

<pre><code>@Path("/student")
public class StudentsResource {
    // this is a mapper class
    @Context
    UriInfo uriInfo;
    @Context
    Request request;

    StudentService studentService;

    public StudentsResource() {
        studentService = new StudentService();
    }

    @GET
    @Produces("text/xml;charset=utf-8")
    public List&lt;Student&gt; getStudents() {
        return studentService.getStudentAsList();
    }
    ...        
}
</code></pre>

<p>Based on Mateen Momin's feedback, I now have this outputting by the REST service on GET /student:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;studentDao_Students&gt;
    &lt;student&gt;
        &lt;name&gt;Jane&lt;/name&gt;
        &lt;grade&gt;B&lt;/grade&gt;
    &lt;/student&gt;
    ...
&lt;/studentDao_Students&gt;
</code></pre>

<p>I have a StudentDao, which gets wrapped by a StudentService, which ultimately gets called by resource classes to make the HTTP calls possible.  I have moved my student class into StudentDao, which made me realize that the StudentService is what is actually getting marshalled, here are updated snippets:</p>

<p><strong>StudentDao.java</strong>:</p>

<pre><code>public enum StudentDao {
    instance;
    private Map&lt;String, Student&gt; students = new HashMap&lt;String, Student&gt;();

    private StudentDao() {

        //pre-populate for testing purposes
        Student student = new Student("John", "A");
        students.put("John", student);
        student = new Student("Jane", "B");
        students.put("Jane", student);
        student = new Student("TESTUSER", "F");
        students.put("TESTUSER", student);
    }

    public Map&lt;String, Student&gt; getStudents() {
        return students;
    }

    @XmlRootElement(name="student")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Student {
        private String name;
        private String grade;

        public Student() {
        }

        public Student(String name, String grade) {
            super();
            this.name = name;
            this.grade = grade;
        }
        // getters/setters here...
    }
}
</code></pre>

<p><strong>StudentService.java</strong>:</p>

<pre><code>@XmlRootElement(name = "student-list")
@XmlAccessorType(XmlAccessType.FIELD)
public class StudentService {

    StudentDao studentDao;

    public StudentService() {
        studentDao = StudentDao.instance;
    }
    // CRUD methods here which call the StudentDao HashMap
}
</code></pre>

<p><strong>StudentsResource.java</strong>:</p>

<pre><code>@Path("/student")
public class StudentsResource {
    @Context
    UriInfo uriInfo;
    @Context
    Request request;

    StudentService studentService;

    public StudentsResource() {
        studentService = new StudentService();
    }

    @GET
    @Produces("text/xml;charset=utf-8")
    public List&lt;Student&gt; getStudents() {
        return studentService.getStudentAsList();
    }

    // other HTTP calls, POST,PUT,etc. here        
}
</code></pre>

<p>I created a main function to see what happens if I specify the JAXBContext:</p>

<p><strong>main for testing console output</strong>:</p>

<pre><code>public class Main {
    public static void main(String[] args) {
        StudentService studentDao = new StudentService();        

        StudentDao.Student stu1 = new StudentDao.Student();
        stu1.setName("marc");
        stu1.setGrade("A");
        StudentDao.Student stu2 = new StudentDao.Student();
        stu2.setName("jacob");
        stu2.setGrade("B");
        StudentDao.Student stu3 = new StudentDao.Student();
        stu3.setName("anthony");
        stu3.setGrade("A");

        studentDao.createStudent(stu1);
        studentDao.createStudent(stu2);
        studentDao.createStudent(stu3);


        try {

            File file = new File("out.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(StudentService.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(studentDao, file);
            jaxbMarshaller.marshal(studentDao, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
</code></pre>

<p>When I run this main function to see the output I actually get this, which does NOT match the REST output of course:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;student-list&gt;
    &lt;studentDao&gt;instance&lt;/studentDao&gt;
&lt;/student-list&gt;
</code></pre>

<p>Which seems promising, but I cannot get seem to get the top level class name to be overridden in the REST calls, which is ultimately what I need.</p>

<p>Any other ideas of what I'm doing wrong here ?</p>