title: Nesting and referencing xml file with JAXB
body: <p>First of all, I am very new to xml world. I am facing problem to extract data from a cross-referenced xml file. Following is an example (modified): </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Student info and students enrollment --&gt; 
  &lt;StudentList&gt;
      &lt;Student_info&gt;
      &lt;Student id = "1"&gt;
          &lt;Name&gt;Mike&lt;/Name&gt;
          &lt;Age&gt;16&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="2"&gt;
          &lt;Name&gt;Matteo&lt;/Name&gt;
          &lt;Age&gt;15&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="3"&gt;
          &lt;Name&gt;Matt&lt;/Name&gt;
          &lt;Age&gt;17&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="4"&gt; 
          &lt;Name&gt;Siri&lt;/Name&gt;
          &lt;Age&gt;16&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="5"&gt;
         &lt;Name&gt;Sara&lt;/Name&gt;
         &lt;Age&gt;15&lt;/Age&gt;
         &lt;/Student&gt; 
      &lt;/Student_info&gt;
  
      &lt;Course_info&gt;
         &lt;Course id="PHY101"&gt;
              &lt;Title&gt;Physics Fundamentals&gt;&lt;/Title&gt;
              &lt;!-- Reference of the students enrolled in physics course --&gt;
              &lt;Student refid ="1"/&gt;
              &lt;Student refid = "2"/&gt;
              &lt;Student refid = "5"/&gt;
          &lt;/Course&gt;
  
      &lt;Course id = "MATH101"&gt;
          &lt;Title&gt;Mathematics Basics&lt;/Title&gt;
          &lt;!-- Reference of the students enrolled in mathematics course --&gt;
          &lt;Student refid = "2"/&gt;
          &lt;Student refid = "3"/&gt;
          &lt;Student refid = "4"/&gt; 
      &lt;/Course&gt;
      &lt;/Course_info&gt;
  &lt;/StudentList&gt;
  </code></pre>
  
  <p>Now, I would like to get the following output using java:</p>
  
  <pre><code>Course Title: Physics Fundamentals
  Name of the enrolled students = Mike, Matteo and Sara
  </code></pre>
  
  <p>And the same for the other course. How can I do that efficiently using <strong>java</strong>? My main question is to how to extract information from a cross-referenced element (such as <strong>Student refid ="1"</strong> in the example)?</p>
  
  <p><strong>An solution that I have found:</strong></p>
  
  <p>Firstly the xml file should be modified as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Student info and students enrollment --&gt; 
  &lt;StudentList&gt;
      &lt;Student_info&gt;
      &lt;Student id = "1"&gt;
          &lt;Name&gt;Mike&lt;/Name&gt;
          &lt;Age&gt;16&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="2"&gt;
          &lt;Name&gt;Matteo&lt;/Name&gt;
          &lt;Age&gt;15&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="3"&gt;
          &lt;Name&gt;Matt&lt;/Name&gt;
          &lt;Age&gt;17&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="4"&gt; 
          &lt;Name&gt;Siri&lt;/Name&gt;
          &lt;Age&gt;16&lt;/Age&gt;
      &lt;/Student&gt;
      &lt;Student id="5"&gt;
         &lt;Name&gt;Sara&lt;/Name&gt;
         &lt;Age&gt;15&lt;/Age&gt;
         &lt;/Student&gt; 
      &lt;/Student_info&gt;
  
      &lt;Course_info&gt;
         &lt;Course id="PHY101"&gt;
              &lt;Title&gt;Physics Fundamentals&gt;&lt;/Title&gt;
              &lt;!-- Reference of the students enrolled in physics course --&gt;
              &lt;!-- Make change here --&gt;
              &lt;Student&gt;1&lt;/Student&gt;
              &lt;Student&gt;2&lt;/Student&gt;
              &lt;Student&gt;5&lt;/Student&gt;
          &lt;/Course&gt;
  
      &lt;Course id = "MATH101"&gt;
          &lt;Title&gt;Mathematics Basics&lt;/Title&gt;
          &lt;!-- Reference of the students enrolled in mathematics course --&gt;
          **&lt;!-- Make change here --&gt;**
          &lt;Student&gt;3&lt;/Student&gt;
          &lt;Student&gt;4&lt;/Student&gt;    
      &lt;/Course&gt;
      &lt;/Course_info&gt;
  &lt;/StudentList&gt;
  </code></pre>
  
  <p>Now, Student class:</p>
  
  <pre><code>import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlID;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  
  @XmlRootElement(name = "Student")
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Student
  {
  
      private String Name;
      private int Age;
      @XmlAttribute(name = "id")
      @XmlID
      private String id;
  
  
      public String getName()
      {
          return Name;
      }
  
      public void setName(String name)
      {
          Name = name;
      }
  
      public int getAge()
      {
          return Age;
      }
  
      public void setAge(int age)
      {
          Age = age;
      }
  
      public String getId()
      {
          return id;
      }
  
      public void setId(String id)
      {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>Course class:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlIDREF;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  
      @XmlRootElement(name = "Course")
      class Course
      {
          @XmlAttribute(name = "cid")
          String id;
          @XmlElement(name = "Title")
          String Title;
  
          @XmlElement(name="Student")
          @XmlIDREF
          List&lt;Student&gt; student;
  
          Course(){
              student = new ArrayList&lt;Student&gt;();
          }
  
          String getID()
          {
              return id;
          }
  
          String getTitle()
          {
              return Title;
          }
  
          void setID(String id)
          {
              this.id = id;
          }
  
          void setTitle(String title)
          {
              this.Title = title;
          }
  
  
          List&lt;Student&gt; getStudent()
          {
              return student;
          }
  
          void setStudents(List&lt;Student&gt; student)
          {
              this.student = student;
          }
  
  
  
   }
  </code></pre>
  
  <p>StudentList class:</p>
  
  <pre><code>import java.util.List;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement(name = "StudentList")
  public class StudentList
  {
  
      private List&lt;Student&gt; Student_info;
      private List&lt;Course&gt; Course_info;
  
      @XmlElement(name = "Student")
      public List&lt;Student&gt; getStudent_info()
      {
          return Student_info;
      }
  
      public void setStudent_info(List&lt;Student&gt; Student_info)
      {
          this.Student_info = Student_info;
      }
  
      @XmlElement(name = "Course")
      public List&lt;Course&gt; getCourse_Info()
      {
          return Course_info;
      }
  
      public void setCourse_Info(List&lt;Course&gt; Course_info)
      {
          this.Course_info = Course_info;
      }
  }
  </code></pre>
  
  <p>TextXml class:</p>
  
  <pre><code>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.Marshaller;
  import javax.xml.bind.Unmarshaller;
  
      public class TextXml {
            public static void main(String[] args)
              {
  
                  try
                  {
  
  
  
                      JAXBContext jc = JAXBContext.newInstance(StudentList.class);
                      Unmarshaller unmarshaller = jc.createUnmarshaller();
  
                      File xml = new File("NewFile.xml");
                      StudentList studentlist = (StudentList) unmarshaller.unmarshal(xml);
  
                      for (Course course: studentlist.getCourse_Info()){
                          System.out.println("Course Title: "+course.getTitle());
                          for(Student std: course.getStudent()){
                              System.out.print(std.getName()+" ");
                          }
                          System.out.println();
                      }
  
                  }
                  catch (JAXBException e)
                  {
                      e.printStackTrace();
                  }
  
              }
          }
  </code></pre>
  
  <p>Thanks.</p>