繁体   English   中英

在Java中浏览对象

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM