2

这会很长,但我需要一些启发。我是 JAXB 的新手,所以请对我宽容。

课程应用:

package Courses;

import java.io.File;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class CoursesApp {
    public static void main(String[] args) {
            Courselist courselist = new Courselist();
            courselist.setclassType("Lecture");
            courselist.setcourseCode("2002");
            courselist.setgroupIndex("1");
            courselist.setprofessor("Professor James");
        try{
            File file = new File("C:\\Courselist.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Courselist.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(courselist, file);
            jaxbMarshaller.marshal(courselist, System.out);

        }catch(JAXBException e)
        {
            e.printStackTrace();
        }
    }
}

课程表:

package Courses;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Courselist {

    String courseCode;
    String classType;
    String groupIndex;
    String professor;

    public String getcourseCode() {
        return courseCode;
    }

    @XmlElement
    public void setcourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public String getclassType() {
        return classType;
    }

    @XmlElement
    public void setclassType(String classType) {
        this.classType = classType;
    }

    public String getgroupIndex() {
        return groupIndex;
    }

    @XmlElement
    public void setgroupIndex(String groupIndex) {
        this.groupIndex = groupIndex;
    }

    public String getprofessor() {
        return professor;
    }

    @XmlElement
    public void setprofessor(String professor) {
        this.professor = professor;
    }
}

输出:

  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <courselist>
  <classType>Lecture</classType> 
  <courseCode>2002</courseCode> 
  <groupIndex>1</groupIndex> 
  <professor>Professor James</professor> 
  </courselist>

我想要的是在同一个 XML 中创建另一个课程列表实例:

  <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <courselist>
  -<course>
     <classType>Lecture</classType> 
     <courseCode>2002</courseCode> 
     <groupIndex>1</groupIndex> 
     <professor>Professor James</professor>
   </course>
  -<course>
     <classType>Lecture</classType> 
     <courseCode>2003</courseCode> 
     <groupIndex>2</groupIndex> 
     <professor>Professor John</professor> 
   </course>
  </courselist>
4

2 回答 2

2

我建议在 CourseList 中有一个成员:List<Course>何时Course将包括当前在CourseList.
这是代码:

@XmlRootElement
public class Courselist {
    @XmlElement List<Course> course = new ArrayList<Course>();
}
于 2012-11-10T21:19:51.983 回答
0

课程表

正如oshai 回答的那样,我将有一个具有两个类CourselistCourse. 下面是这个Courselist类的样子。为了匹配 Java 编程约定,包名通常是小写的。此外,它也经常基于域名(例如com.example.courses)。默认情况下,JAXB(JSR-222)get实现在属性(或方法)上查找元数据,set所以我把它们放在那里(参见:http ://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to .html)。

package courses;

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Courselist {

    List<Course> courses;

    @XmlElement(name="course")
    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }

}

课程

你在Courselist课堂上的信息我已经搬到了一个新的Course班级。JAXB 是异常配置,因此您只需在希望 XML 表示不同于默认值的地方添加注释。在您的用例中,您不需要对此类进行任何注释(请参阅:http ://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html )。我已经修复了您的属性方法的大小写以匹配正常的 Java 编码约定。

package courses;

public class Course {

    String courseCode;
    String classType;
    String groupIndex;
    String professor;

    public String getCourseCode() {
        return courseCode;
    }

    public void setCourseCode(String courseCode) {
        this.courseCode = courseCode;
    }

    public String getClassType() {
        return classType;
    }

    public void setClassType(String classType) {
        this.classType = classType;
    }

    public String getGroupIndex() {
        return groupIndex;
    }

    public void setGroupIndex(String groupIndex) {
        this.groupIndex = groupIndex;
    }

    public String getProfessor() {
        return professor;
    }

    public void setProfessor(String professor) {
        this.professor = professor;
    }

}
于 2012-11-10T23:22:42.877 回答