[英]Navigation through Objects in Java
我的目標是在自定義對象中重新創建XML的結構,以進一步對其進行操作。 實際上,我想將XML作為輸入並產生LaTeX作為輸出。 為此,我實現了JAXB庫的原理。 但是不要以為這是個好主意,因為將所需的文檔結構保留為TeX中的輸出並不方便。
這是我的自定義類的示例:
public class Section {
private String title;
private List<Par> par;
private List<SubSec> subsec;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = "\\section {" + title + "}";
}
public List<Par> getPar() {
if (par == null) {
par = new ArrayList<Par>();
}
return this.par;
}
public List<SubSec> getSubSec() {
if (subsec == null) {
subsec = new ArrayList<SubSec> ();
}
return this.subsec;
}
}
因此,我有一個Section類的列表,這些類具有標題,段落列表(Par)和子節列表(SubSec)(簡化LaTeX文章結構)。 段落包含文本,但小節也可以包含段落列表。 輸入XML后,我將所有數據從該數據傳輸到對象(此類的實例)中。
例如:
List<Section> listSections = new ArrayList<Section>();
// omitting the actions to recreate the structure and set values to Objects
// now, to retrieve and write:
for (int j = 0; j < listSections.size(); j++) {
List<Par> listParText = listSections.get(j).getPar();
writer.write(listSections.get(j).getTitle());
writer.newLine();
for (Par parList : listParText) {
if (parList.getText() != null) {
writer.write(parList.getText());
writer.newLine();
}
}
}
問題是,我無法在舞台自定義對象-> TeX上重新創建文檔的結構。 盡管結構保留在舞台XML上-自定義對象。 在對象模型中,例如:
Section1(title): Par(text), Par(text), Par(text)
Section2(title): Subsection1(title): Par(text), Par(text), Par(text)
Subsection2(title): Par(text), Par(text)
Section3(title): Par(text)
有沒有一種方法可以保存此訂單並以相同的順序獲取價值以將其寫入文件? 用getter和setter來獲取值對我來說不是問題,以適當的順序檢索它們是有問題的。
更新為闡明問題,讓我們假設每個小節都包含按特定順序排列的段落(Par),小節(SubSec),表格和圖形。 但顯然Java不允許創建類似List<SubSec, Par, Table, Fig>
的列表: List<SubSec, Par, Table, Fig>
。 我可以按一定順序放置信息,但不能檢索。 可以嗎
創建父類(例如DocumentComponent),它的SubSec,Par,Table和Fig都是子類,然后說一個文檔是DocumentComponents的有序列表,這會起作用嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.