Java 用JAXB嵌套和引用xml文件

Java 用JAXB嵌套和引用xml文件,java,xml,xsd,xml-parsing,jaxb,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<

首先,我对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高效地实现这一点?我的主要问题是如何从示例中的交叉引用元素(如Student refid=1)中提取信息

我找到了一个解决方案:

首先,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 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;
        }



 }
谢谢

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

请看我的另一个解决方案,它正在工作。请投票支持我。你测试了代码吗?我的代码返回了一个错误。您是否已按上述方式更改了xml文件路径和xml格式。请使用上述文件创建独立的java项目,然后运行,您将获得结果。我已经对它进行了测试。我检查了所有内容,基本上,你的xml文件和我的xml文件没有什么不同。错误不在文件路径中,而是关于javax.xml.bind.unmarshaleException-链接异常:[org.xml.sax.SAXParseException;systemId:file:/C:/Users/mahbub/workspace/energyplanguai/NewFile.xml;行号:1;列号:7;不允许处理指令目标与[xX][mM][lL]匹配。]应该是xml文件的第一行,所以请删除上面的任何空行或空格。
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;
        }



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

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

    }
}