简体   繁体   English

使用Sax Parser解析xml中的父/子关系

[英]Parent/Child relationship in parsing xml with Sax Parser

I have seen a question for this already but it is not answered and I hope that this time I get lucky. 我已经看到了有关此问题的信息,但尚未得到答复,希望这次我能走运。 As the question states I am trying to parse the xml with SAX Parser and need to maintain parent/child relationship. 正如问题所述,我试图使用SAX Parser解析xml,并且需要维护父/子关系。 As SAX Parser is context free so I need to create some context of my own. 由于SAX Parser不受上下文限制,因此我需要创建自己的上下文。 I am half way through but stuck at this point. 我已经完成了一半,但目前仍停留在此。 Below is the sample structure of my xml, 下面是我的xml的示例结构,

<Folder name="Folder1" nodeId="1">
<Level1 name="NodeA1" nodeId="2">
    <Level2 name="NodeA11" ImageId="Image11.png" nodeId="2" />
    <Level2 name="NodeA12" ImageId="Image12.png" nodeId="2" />    
</Level1>
<Level1 name="NodeA2" nodeId="3">
    <Level2 name="SubNodeA2" nodeId="131">
        <Level3 name="NodeA21" ImageId="Image21.png" nodeId="131" />  
    </Level2>
    <Level2 name="NodeA13" ImageId="Image13.png" nodeId="3" />
    <Level2 name="NodeA14" ImageId="Image14.png" nodeId="3" />
</Level1>
</Folder>
<Folder name="Folder2" nodeId="2">
<Level1 name="NodeB1" nodeId="2">
    <Level2 name="Node1B1" ImageId="Image11.png" nodeId="2" />
    <Level2 name="Node1B2" ImageId="Image12.png" nodeId="2" />    
</Level1>
<Level1 name="NodeB2" nodeId="3">
    <Level2 name="SubNodeB2" nodeId="131">
        <Level3 name="NodeB21" ImageId="Image21.png" nodeId="131" />  
    </Level2>
    <Level2 name="NodeB13" ImageId="Image13.png" nodeId="3" />
    <Level2 name="NodeB14" ImageId="Image14.png" nodeId="3" />
</Level1>

An the property class to store the xml structure, 一个用于存储xml结构的属性类,

private String nodeName;
private String name;
private String ImageId;
private int nodeId;
private String folderName;

In the Default handler the following code of startElement is used to process the node and attributes, 在默认处理程序中,以下startElement代码用于处理节点和属性,

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        ListItems_Nodes node= new ListItems_Nodes();

        if (qName.equalsIgnoreCase("Folder")) {
            FolderName = attributes.getValue("name").toString();
        }

        nextLevelNode.setFolderName(FolderName);
        nextLevelNode.setNodeName(qName);
        nextLevelNode.setName(attributes.getValue("name").toString();
        nextLevelNode.setChartId(attributes.getValue("ImageId").toString());
        nextLevelNode.setNodeId(Integer.parseInt(attributes.getValue("nodeId")));

        NodeLevelValues.add(nextLevelNode);
}

I can access the nodes with ImageId of any folder because they have the same nodeId as the parent node but if a node has a sub node which is not Image then its nodeId is different from the parent node and I am unable to access particular sub-nodes. 我可以使用任何文件夹的ImageId访问节点,因为它们的节点ID与父节点相同,但是如果节点的子节点不是Image,则其nodeId与父节点不同,并且我无法访问特定的子节点。节点。 Now inside the startElement I am thinking to store the parent name with each node Id. 现在,在startElement内部,我正在考虑将父名称与每个节点ID一起存储。 I have implemented this for Folder node and it is working fine but for other nodes like Level1, Level2 .... Level(n) I am unable to find a way to implement this. 我已经为Folder节点实现了此功能,并且工作正常,但对于其他节点,例如Level1,Level2...。Level(n)我无法找到实现此目的的方法。

Any help will be highly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

It seems like no one interested in answering the question. 似乎没人愿意回答这个问题。 I found out that this was not a dumb question and may be no one knew how to solve this. 我发现这不是一个愚蠢的问题,也许没人知道如何解决这个问题。 I found very informative and interesting article about having Document Order Indices and this article solved my problem of parent/child relationship. 我发现有关文档顺序索引的文章非常有用且有趣,这篇文章解决了我的父子关系问题。 Not only this solved my issue but also due to this my more then 100 lines of codes (checks/conditions) are now shortened to only 6 to 7 lines of codes. 这不仅解决了我的问题,而且由于这个原因,我的100多行代码(检查/条件)现在缩短为仅6至7行代码。 The article can be found out at http://www.ibm.com/developerworks/library/x-tipsaxdo2/ . 可以在http://www.ibm.com/developerworks/library/x-tipsaxdo2/中找到该文章。

I will briefly described what it has changed in my application. 我将简要描述它在我的应用程序中发生了什么变化。 Introduced two global variables, 引入了两个全局变量,

private int m_nodeIx;
private Stack<Integer> m_parentStack;

Also added two more fields to my property class, 还向我的媒体资源类添加了两个字段,

private int nodeIndex;
private int parentIndex;

Added startDocument method for my DefaultHandler, 为我的DefaultHandler添加了startDocument方法,

public void startDocument() throws SAXException {
    m_nodeIx = -1;
    m_parentStack = new Stack<Integer>();
    m_parentStack.push(Integer.valueOf(m_nodeIx));
}

In the startElement, 在startElement中,

++ m_nodeIx;
int parentNodeIx = ((Integer) m_parentStack.peek()).intValue();

At the end of populating all the property fields, 在填充所有属性字段的最后,

propertyClass.setNodeIndex(m_nodeIx);
propertyClass.setParentIndex(parentNodeIx);
m_parentStack.push(Integer.valueOf( m_nodeIx ));

And finally inside my endElement, 最后在我的endElement中,

m_parentStack.pop();

So if any body wants to have same functionality, they can easily understand what is being done here. 因此,如果任何机构都希望具有相同的功能,那么他们可以轻松了解此处正在执行的操作。 (1) Every node is now assigned an Index. (1)现在为每个节点分配了一个索引。 (2) Every child node has the parent Index equals to the node index of parent node. (2)每个子节点的父索引等于父节点的节点索引。 I hope that this is useful for anyone. 我希望这对任何人都有用。 (Disappointed but happy to solve it myself) (很失望,但很乐意自己解决)

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

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