【发布时间】:2014-07-25 14:31:41
【问题描述】:
我正在尝试编组标签不断变化的标签。我继续覆盖对象值,并在编组时得到相同的标签值。
期望的输出:
<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 语句在迭代包含从数据库中提取的值的数组列表时创建对象。我最终覆盖了最后一个对象,所以我正在寻找一种方法来保存一组对象,这些对象填充在我的迭代器的每次传递中。
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 Schema 文件什么都做不了。您可以将其简化为书籍和杂志及其顶级元素“作者”。
-
我无法编辑架构,我必须使用给定的架构并生成一个可以使用该架构进行验证的 XML 文件。
-
如果您的架构阻止您正确子类化基类型以获得您想要的东西,那么您正在做的事情将很难实现。您可以通过在您的类中添加
List<Book>和List<Magazine>并使用您的工厂相应地填充每个列表来实现您所需要的,而不是依赖于多态性。
标签: java xml object jaxb marshalling