使用JAXB

时间:2017-03-28 12:27:15

标签: java xml xsd xml-parsing jaxb

首先,我对xml世界很新。我面临着从交叉引用的xml文件中提取数据的问题。以下是一个例子(修改):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Student info and 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 the students enrolled in physics course -->
            <Student refid ="1"/>
            <Student refid = "2"/>
            <Student refid = "5"/>
        </Course>

    <Course id = "MATH101">
        <Title>Mathematics Basics</Title>
        <!-- Reference of the students enrolled in mathematics course -->
        <Student refid = "2"/>
        <Student refid = "3"/>
        <Student refid = "4"/> 
    </Course>
    </Course_info>
</StudentList>

现在,我想使用java获得以下输出:

Course Title: Physics Fundamentals
Name of the enrolled students = Mike, Matteo and Sara

其他课程也一样。如何使用 java 有效地完成这项工作?我的主要问题是如何从交叉引用的元素中提取信息(例如学生refid =&#34; 1&#34; ?)

我找到的解决方案:

首先应修改xml文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Student info and 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 the 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 the students enrolled in mathematics course -->
        **<!-- Make change here -->**
        <Student>3</Student>
        <Student>4</Student>    
    </Course>
    </Course_info>
</StudentList>

现在,学生班:

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

课程类:

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类:

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类:

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

            }
        }

感谢。

1 个答案:

答案 0 :(得分:0)

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Student info and 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 the students enrolled in physics course -->
            <Student refid ="1"/>
            <Student refid = "2"/>
     </Course>
     <Course id = "MATH101">
        <Title>Mathematics Basics</Title>
        <!-- Reference of the 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);
            for (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();
        }

    }
}