Reputation: 853
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. As SAX Parser is context free so I need to create some context of my own. I am half way through but stuck at this point. Below is the sample structure of my 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,
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,
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. Now inside the startElement I am thinking to store the parent name with each node 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.
Any help will be highly appreciated. Thanks
Upvotes: 0
Views: 1577
Reputation: 853
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. The article can be found out at 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,
public void startDocument() throws SAXException {
m_nodeIx = -1;
m_parentStack = new Stack<Integer>();
m_parentStack.push(Integer.valueOf(m_nodeIx));
}
In the 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,
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. (2) Every child node has the parent Index equals to the node index of parent node. I hope that this is useful for anyone. (Disappointed but happy to solve it myself)
Upvotes: 1