【问题标题】:Nesting and referencing xml file with JAXB使用 JAXB 嵌套和引用 xml 文件
【发布时间】:2017-08-21 12:40:35
【问题描述】:

首先,我对 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 有效地做到这一点?我的主要问题是如何从交叉引用的元素中提取信息(例如示例中的 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 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();
                }

            }
        }

谢谢。

【问题讨论】:

  • 请查看我正在运行的另一个解决方案,请为我投票。

标签: java xml xsd xml-parsing jaxb


【解决方案1】:
    <?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();
        }

    }
}

【讨论】:

  • 你测试过代码吗?我的代码返回错误。
  • 你是否改变了上面提到的xml文件路径和xml格式。请用上面的文件制作你的独立java项目,然后运行你会得到结果。我已经测试过了。
  • 我检查了一切,基本上你和我的xml文件没有什么不同。错误不在文件路径中,它关于“javax.xml.bind.UnmarshalException - 带有链接异常:[org.xml.sax.SAXParseException; systemId: file:/C:/Users/mahbub/workspace/EnergyPlanGUI/NewFile .xml; lineNumber: 1; columnNumber: 7; 不允许匹配“[xX][mM][lL]”的处理指令目标。]”
  • 应该是您的 xml 文件的第一行,因此请删除其上方的任何空白行或空格。
猜你喜欢
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
相关资源
最近更新 更多