简体   繁体   English

在Java中浏览对象

[英]Navigation through Objects in Java

My aim is to recreate the structure of XML in custom Objects to operate with it further. 我的目标是在自定义对象中重新创建XML的结构,以进一步对其进行操作。 Actually, I want to have XML as input and produce LaTeX as output. 实际上,我想将XML作为输入并产生LaTeX作为输出。 For this task I have implemented principles of JAXB library. 为此,我实现了JAXB库的原理。 But don't think that this is a good idea, because it is not convenient to retain the needed structure of document as output in TeX. 但是不要以为这是个好主意,因为将所需的文档结构保留为TeX中的输出并不方便。

Here is an example of my custom class: 这是我的自定义类的示例:

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;
    }

}

So I have a list of Section class, which have titles, list of paragraphs (Par) and list of subsections (SubSec) (simplify LaTeX article structure). 因此,我有一个Section类的列表,这些类具有标题,段落列表(Par)和子节列表(SubSec)(简化LaTeX文章结构)。 Paragraphs contain text, but subsection can include also list of paragraphs. 段落包含文本,但小节也可以包含段落列表。 After XML input I transfer all Data from it in objects, instances of this Classes. 输入XML后,我将所有数据从该数据传输到对象(此类的实例)中。

As example: 例如:

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();
         }
     }
}

The problem is, that I can't recreate the structure of the document on the stage custom objects -> TeX. 问题是,我无法在舞台自定义对象-> TeX上重新创建文档的结构。 Although the structure is preserved on stage XML - custom objects. 尽管结构保留在舞台XML上-自定义对象。 In Objects model I have, for example: 在对象模型中,例如:

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)

Is there a way to save this order and get value in the same order to write them to file? 有没有一种方法可以保存此订单并以相同的顺序获取价值以将其写入文件? Get values with getters and setters is NOT a problem to me, problem to retrieve them with proper order. 用getter和setter来获取值对我来说不是问题,以适当的顺序检索它们是有问题的。

Update To clarify the problem, lets suppose every Section contains paragraphs (Par), subsection (SubSec), Tables, Figures in certain order. 更新为阐明问题,让我们假设每个小节都包含按特定顺序排列的段落(Par),小节(SubSec),表格和图形。 But obviously Java not allow to make a list like: List<SubSec, Par, Table, Fig> . 但显然Java不允许创建类似List<SubSec, Par, Table, Fig>的列表: List<SubSec, Par, Table, Fig> I can put information there in certain order, but not retrieve. 我可以按一定顺序放置信息,但不能检索。 Or can I? 可以吗

创建父类(例如DocumentComponent),它的SubSec,Par,Table和Fig都是子类,然后说一个文档是DocumentComponents的有序列表,这会起作用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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