![](/img/trans.png)
[英]Can you have multiple sets of JAXB annotations to marshal/unmarshal to different XML?
[英]How do you Marshal multiple sets of objects with different values in JAXB?
我正在嘗試封送標記不斷變化的標記。 編組時,我會繼續覆蓋對象值,並最終獲得與標簽相同的值。
所需的輸出:
<bookshelf>
<shelfnumber> 001 </shelfnumber>
<shelfowner> John Snow </shelfowner>
<book>
<Author>Ned Stark</Author>
<Chapters>24</Chapters>
<Chapter1>.......</Chapter1>
<Chapter2>.......</Chapter2>
</book>
<book>
<Author>Rob Stark</Author>
<Chapters>24</Chapters>
<Chapter1>.......</Chapter1>
<Chapter2>.......</Chapter2>
</book>
<magazine>
<Author>Tyrion Lannister</Author>
<Pages>24</Pages>
<Page1>.......</Page1>
<Page2>.......</Page2>
</magazine>
</bookshelf>
我有一個switch語句在遍歷包含從數據庫中提取的值的arraylist時創建對象。 我最終覆蓋了最后一個對象,因此我正在尋找一種方法來保存在迭代器的每個遍歷中填充的一組對象。
public class TagFactory {
/*
* Creates an arraylist from which to read the values and populate the tags
*/
private ArrayList<Data> Values;
public TagFactory(ArrayList<Data> values) {
Values = values;
}
/*
* Populates the Tran tags for the output based on the values in the Vales
* arraylist. Uses the index of each cell to match up with the corresponding
* tags
*/
public Tran TagPopulator() {
BookShelf bookshelf = new BookShelf();
Magazine mag = new Magazine();
Book book = new book();
Page page = new Page();
Chapter chapter = new Chapter();
/*
* Create iterator and iterate though the extracted data arraylist
*/
/*
* Static Fields constructors
*/
bookshelf.setNumber(BigInteger.valueOf(6));
bookshelf.setAuthor("John Snow");
...
Iterator<Data> ValuesIt = Values.iterator();
boolean Endofchapter = false;
boolean Endofpage = false;
while (ValuesIt.hasNext()) {
/*
* Data object to hold the values
*/
Data data = ValuesIt.next();
/*
* Select the appropriate class object constructor based on the
* index of the cell This index was previously matched up in
* HeaderValues and is used for a reference Type modification was
* done for each of the constructor's requirements.
*/
switch (data.getcellIndex()) {
// Book->Author
case 0:
book.setAuthor(data.getcellValue());
break;
// Book->Chapter-> Chap #
case 1:
Chapter.add(data.getcellValue());
...
Endofchapter = true;
break;
// Mag -> Author
case 2:
mag.setAuthor(data.getcellValue());
break;
// Mag->Page->Page#
case 3:
/*
* Page object created and modified
*/
page.setnum(data.getcellValue());
Endofpage = true;
break;
...
if(Endofpage){
mag.add(page);
}
if(Endofchapter){
book.add(chapter);
}
bookshelf.add(mag);
bookshelf.add(book)
...
循環完成后,我正在編組。
添加了封送處理功能
public class XMLWriter {
private String FileOutput;
private Tran Transaction;
...
public void FileOut () {
try {
/*
* Marshal the classes into and XML output
*/
File file = new File(FileOutput);
JAXBContext JC = JAXBContext.newInstance(Tran.class);
Marshaller JCMarshaller = JC.createMarshaller();
JCMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JCMarshaller.marshal(Transaction, file);
JCMarshaller.marshal(Transaction, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
將調用添加到用於編寫XML的類。
XMLWriter output = new XMLWriter(outputFilename, newBookshelf.TagPopulator());
output.FileOut();
除XMLWriter和TagFactory之外的所有對象均由JAXB通過模式生成。
由於我們沒有顯示XML模式,因此下面的代碼可能不完全適合。 但是,它確實會根據XML示例生成有效的XML。 列表由掃描儀代替,從文本文件讀取。 您對while / switch的意圖對我不太清楚。 我無法想象最后的這些add()調用可能如何正常工作。
請注意,在將DOM樹中的元素插入其父級之前,無需完成表示該元素的對象。 要重復添加,請保留對最新/當前書籍或雜志的引用。
可能還有另一種方法來確定章節和頁面的數量。 我完全不知道在章節或頁面中應該發生什么。
BookShelf bookshelf = new BookShelf();
bookshelf.setShelfnumber( "001" );
bookshelf.setShelfowner( "John Doe" );
File f = new File( "data.txt" );
Scanner scanner = new Scanner( f );
Book book = null;
Magazine magazine = null;
int chapters = 0;
int pages = 0;
while( scanner.hasNext() ){
int cellid = scanner.nextInt();
switch(cellid){
case 0:
book = new Book();
bookshelf.getBookOrMagazine().add( book );
book.setAuthor( scanner.nextLine() );
chapters = 0;
break;
case 1:
Chapter chapter = new Chapter();
book.getChapter().add( chapter );
chapter.setNumber( scanner.nextInt() );
book.setChapters( ++chapters );
break;
case 2:
magazine = new Magazine();
bookshelf.getBookOrMagazine().add( magazine );
magazine.setAuthor( scanner.nextLine() );
pages = 0;
break;
case 3:
Page page = new Page();
magazine.getPage().add( page );
page.setNumber( scanner.nextInt() );
magazine.setPages( ++pages );
break;
}
}
我使用的XML模式文件:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.0">
<xs:element name="bookshelf" type="BookShelf"/>
<xs:complexType name="BookShelf">
<xs:sequence>
<xs:element name="shelfnumber" type="xs:string"/>
<xs:element name="shelfowner" type="xs:string"/>
<xs:choice maxOccurs="unbounded">
<xs:element name="book" type="Book"/>
<xs:element name="magazine" type="Magazine"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Book">
<xs:sequence>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Chapters" type="xs:int"/>
<xs:element name="chapter" type="Chapter" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Magazine">
<xs:sequence>
<xs:element name="Author" type="xs:string"/>
<xs:element name="Pages" type="xs:int"/>
<xs:element name="page" type="Page" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Chapter">
<xs:sequence>
<xs:element name="number" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Page">
<xs:sequence>
<xs:element name="number" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.