在Java中通过对象导航

时间:2017-03-06 14:38:16

标签: java xml object arraylist latex

我的目标是在自定义对象中重新创建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();
         }
     }
}

问题是,我无法在舞台自定义对象上重新创建文档的结构 - &gt; 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>。我可以按特定顺序放置信息,但不能检索。或者我可以吗?

1 个答案:

答案 0 :(得分:1)

是否可以创建一个父类,比如DocumentComponent,其中SubSec,Par,Table和Fig都是子类,然后说文档是DocumentComponents的有序列表?