2012-11-10 150 views
2

這會很長,但我需要一些啓發。我是JAXB的新手,所以請對我寬大。JAVA/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; 
    } 
} 

輸出:

<?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中創建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> 
+0

您所需的輸出不是有效的XML文檔。 – toniedzwiedz

+0

對不起,我意識到它不是。編輯。 –

+0

是否可以將xml結構更改爲具有'course'元素? – oshai

回答

2

我建議在CourseList中有一個成員:List<Course>Course將包括當前在CourseList中的所有成員。
這是代碼:

@XmlRootElement 
public class Courselist { 
    @XmlElement List<Course> course = new ArrayList<Course>(); 
} 
0

Courselist

正如oshai answered我將有兩班CourselistCourse的典範。以下是Courselist類的樣子。爲了匹配Java編程約定,包名通常是小寫字母。另外它也經常基於域名(例如com.example.courses)。默認情況下,JAXB (JSR-222)實現在屬性上查找元數據(getset方法),所以我將它們放在那裏(請參閱: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; 
    } 

}