java - Nesting and referencing xml file with JAXB -


first of all, new xml world. facing problem extract data cross-referenced xml file. following example (modified):

<?xml version="1.0" encoding="utf-8"?> <!-- student info , students enrollment -->  <studentlist>     <student_info>     <student id = "1">         <name>mike</name>         <age>16</age>     </student>     <student id="2">         <name>matteo</name>         <age>15</age>     </student>     <student id="3">         <name>matt</name>         <age>17</age>     </student>     <student id="4">          <name>siri</name>         <age>16</age>     </student>     <student id="5">        <name>sara</name>        <age>15</age>        </student>      </student_info>      <course_info>        <course id="phy101">             <title>physics fundamentals></title>             <!-- reference of students enrolled in physics course -->             <student refid ="1"/>             <student refid = "2"/>             <student refid = "5"/>         </course>      <course id = "math101">         <title>mathematics basics</title>         <!-- reference of students enrolled in mathematics course -->         <student refid = "2"/>         <student refid = "3"/>         <student refid = "4"/>      </course>     </course_info> </studentlist> 

now, following output using java:

course title: physics fundamentals name of enrolled students = mike, matteo , sara 

and same other course. how can efficiently using java? main question how extract information cross-referenced element (such student refid ="1" in example)?

an solution have found:

firstly xml file should modified follows:

<?xml version="1.0" encoding="utf-8"?> <!-- student info , students enrollment -->  <studentlist>     <student_info>     <student id = "1">         <name>mike</name>         <age>16</age>     </student>     <student id="2">         <name>matteo</name>         <age>15</age>     </student>     <student id="3">         <name>matt</name>         <age>17</age>     </student>     <student id="4">          <name>siri</name>         <age>16</age>     </student>     <student id="5">        <name>sara</name>        <age>15</age>        </student>      </student_info>      <course_info>        <course id="phy101">             <title>physics fundamentals></title>             <!-- reference of students enrolled in physics course -->             <!-- make change here -->             <student>1</student>             <student>2</student>             <student>5</student>         </course>      <course id = "math101">         <title>mathematics basics</title>         <!-- reference of students enrolled in mathematics course -->         **<!-- make change here -->**         <student>3</student>         <student>4</student>         </course>     </course_info> </studentlist> 

now, student class:

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

course class:

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<student> student;          course(){             student = new arraylist<student>();         }          string getid()         {             return id;         }          string gettitle()         {             return title;         }          void setid(string id)         {             this.id = id;         }          void settitle(string title)         {             this.title = title;         }           list<student> getstudent()         {             return student;         }          void setstudents(list<student> student)         {             this.student = student;         }     } 

studentlist class:

import java.util.list; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement;  @xmlrootelement(name = "studentlist") public class studentlist {      private list<student> student_info;     private list<course> course_info;      @xmlelement(name = "student")     public list<student> getstudent_info()     {         return student_info;     }      public void setstudent_info(list<student> student_info)     {         this.student_info = student_info;     }      @xmlelement(name = "course")     public list<course> getcourse_info()     {         return course_info;     }      public void setcourse_info(list<course> course_info)     {         this.course_info = course_info;     } } 

textxml class:

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);                      (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();                 }              }         } 

thanks.

    <?xml version="1.0" encoding="utf-8"?> <!-- student info , students enrollment -->  <studentlist>     <student id = "1">         <name>mike</name>         <age>16</age>     </student>     <student id="2">         <name>matteo</name>         <age>15</age>     </student>     <student id="3">         <name>matt</name>         <age>17</age>     </student>     <student id="4">          <name>siri</name>         <age>16</age>     </student>     <student id="5">        <name>sara</name>        <age>15</age>      </student>      <course id="phy101">         <title>physics fundamentals</title>         <!-- reference of students enrolled in physics course -->             <student refid ="1"/>             <student refid = "2"/>      </course>      <course id = "math101">         <title>mathematics basics</title>         <!-- reference of students enrolled in mathematics course -->             <student refid = "2"/>             <student refid = "3"/>             <student refid = "4"/>        </course>   </studentlist> 

 package test;      import javax.xml.bind.annotation.xmlaccesstype;     import javax.xml.bind.annotation.xmlaccessortype;     import javax.xml.bind.annotation.xmlattribute;     import javax.xml.bind.annotation.xmlrootelement;      @xmlrootelement(name = "student")     @xmlaccessortype(xmlaccesstype.field)     public class student     {          private string name;         private int age;         @xmlattribute(name = "id")         private int id;         @xmlattribute(name = "refid")         private int refid;          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 int getid()         {             return id;         }          public void setid(int id)         {             this.id = id;         }          public int getrefid()         {             return refid;         }          public void setrefid(int refid)         {             this.refid = refid;         }      } 

     package test;      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.xmlrootelement;      @xmlrootelement(name = "course")     @xmlaccessortype(xmlaccesstype.field)     public class course     {         @xmlattribute(name = "id")         private string id;         private string title;         @xmlelement(name = "student")         private list<student> students = null;          public string getid()         {             return id;         }          public string gettitle()         {             return title;         }          public void setid(string id)         {             this.id = id;         }          public void settitle(string title)         {             this.title = title;         }          public list<student> getstudents()         {             return students;         }          public void setstudents(list<student> students)         {             this.students = students;         }     } 

        package test;      import java.util.list;     import javax.xml.bind.annotation.xmlelement;     import javax.xml.bind.annotation.xmlrootelement;      @xmlrootelement(name = "studentlist")     public class studentlist     {          private list<student> student_info;         private list<course> course_info;          @xmlelement(name = "student")         public list<student> getstudent_info()         {             return student_info;         }          public void setstudent_info(list<student> student_info)         {             this.student_info = student_info;         }          @xmlelement(name = "course")         public list<course> getcourse_info()         {             return course_info;         }          public void setcourse_info(list<course> course_info)         {             this.course_info = course_info;         }     } 

  package test;  import java.io.file; import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbexception; import javax.xml.bind.unmarshaller;  public class demo {     public static void main(string[] args)     {          try         {              file file = new file("d:\\xmlfile.xml");             jaxbcontext jaxbcontext = jaxbcontext.newinstance(studentlist.class);              unmarshaller jaxbunmarshaller = jaxbcontext.createunmarshaller();             studentlist studentlist = (studentlist) jaxbunmarshaller.unmarshal(file);             (student emp : studentlist.getstudent_info())             {                 system.out.println(emp.getage());                 system.out.println(emp.getname());                 system.out.println(emp.getid());                 system.out.println(emp.getrefid());             }          }         catch (jaxbexception e)         {             e.printstacktrace();         }      } } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -