[英]JAVA/JAXB Help needed
This is gonna be lengthy but I need some enlightenment.这会很长,但我需要一些启示。 I'm new to JAXB so please be lenient with me.
我是 JAXB 的新手,所以请对我宽容一点。
CourseApp:课程应用:
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();
}
}
}
Courselist:课程表:
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;
}
}
Output:输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <courselist>
<classType>Lecture</classType>
<courseCode>2002</courseCode>
<groupIndex>1</groupIndex>
<professor>Professor James</professor>
</courselist>
What I want is to create another instance of courselist within the same XML:我想要的是在同一个 XML 中创建 courselist 的另一个实例:
<?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>
I would recommend to have one member in CourseList: List<Course>
when Course
will include all the members currently in CourseList
.我建议在 CourseList 中有一个成员:
List<Course>
当Course
将包含当前在CourseList
所有成员时。
This is the code:这是代码:
@XmlRootElement
public class Courselist {
@XmlElement List<Course> course = new ArrayList<Course>();
}
Courselist课程表
As oshai answered I would have a model with two classes Courselist
and Course
.正如oshai 回答的那样,我将有一个包含两个类
Courselist
和Course
的模型。 Below is what the Courselist
class would look like.下面是
Courselist
类的样子。 To match Java programming conventions the package name is normally lower case.为了匹配 Java 编程约定,包名通常是小写的。 Also it is also often based on a domain name (such as
com.example.courses
).此外,它也通常基于域名(例如
com.example.courses
)。 By default JAXB (JSR-222) implementations look for metadata on the property ( get
or set
methods) so I've put them there (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html ).默认情况下, 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;
}
}
Course课程
The information you had in the Courselist
class I have moved to a new Course
class.您在
Courselist
类中的信息我已移至新的Course
类。 JAXB is configuration by exception so you only need to add annotations where you wish the XML representation to differ from the default. JAXB 是异常配置,因此您只需要在您希望 XML 表示与默认值不同的地方添加注释。 In your use case you don't need any annotations on this class (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html ).
在您的用例中,您不需要对此类进行任何注释(请参阅: http : //blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html )。 I have fixed the casing on your property methods to match the normal Java coding conventions.
我已经修复了您的属性方法的大小写以匹配正常的 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.