XML Parsing : use Recursive method.
Please do help me friends ..Struggling a lot!!!
I have an XML File :
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<workspaces>
<workspace>
<id>3</id>
<name>Personal</name>
</workspace>
<workspace>
<id>4</id>
<name>Test1</name>
<workspaces>
<workspace>
<id>5</id>
<name>Child of Test1</name>
</workspace>
</workspaces>
</workspace>
<workspace>
<id>6</id>
<name>Test2</name>
<workspaces>
<workspace>
<id>7</id>
<name>Child of Test 2</name>
</workspace>
</workspaces>
</workspace>
</workspaces>
The code I have written in such a way that it displays on a frame .
But , I have aproblem : I have to write the code in a recursive manner becoz there may be sum situations in the project in future where i cannot guess the depth of the xml file.
For now i get all the nodes in an order but actually 5 is the child of 4 . and even 7 is the child of 6.
The code is as follows :
Code:
package sage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import classes.sage.*;
import javax.swing.tree.TreeNode;
public class NextPage
{
JFrame frame;
JPanel pamel;
JButton okButton;
JButton cancelButton;
JScrollPane leftPane;
JTextArea bottomArea;
JTextArea leftArea;
JTextArea topArea;
MyTreeModel treeModel;
MyTree treePane;
IconNode treeNode;
public NextPage()
{
frame = new JFrame("Upload Dialog Box");
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
bottomArea = new JTextArea(10,20);
bottomArea.setEditable(false);
topArea = new JTextArea(5, 40);
topArea.setEditable(false);
bottomArea = new JTextArea();
leftArea = new JTextArea();
leftPane = new JScrollPane(leftArea);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
}
catch (Exception e)
{
}
Document doc = null;
try {
doc = db.parse("http://dakota-swetha.cwru.edu/remote/workspaces/9HTTjw9qSVGiG2aGsCLC/");
}
catch (Exception e)
{
}
doc.getDocumentElement().normalize();
treeNode = new IconNode(doc.getDocumentElement().getNodeName(), "root");
NodeList nodeLst = doc.getElementsByTagName("workspace");
for (int s = 0; s < nodeLst.getLength(); s++)
{
Node fstNode = nodeLst.item(s); //Get workspace
//Get ID element
Element E = (Element)fstNode;
NodeList fstNmElmntLst = E.getElementsByTagName("id");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
IconNode tn2 = new IconNode(((Node) fstNm.item(0)).getNodeValue(), "child");
treeNode.add(tn2);
//Get Name element
E = (Element)fstNode;
fstNmElmntLst = E.getElementsByTagName("name");
fstNmElmnt = (Element) fstNmElmntLst.item(0);
fstNm = fstNmElmnt.getChildNodes();
IconNode tn3 = new IconNode(((Node) fstNm.item(0)).getNodeValue(), "child");
tn2.add(tn3);
treeNode.add(tn2);
}
treeModel = new MyTreeModel(treeNode);
treePane = new MyTree(treeModel);
frame.setLayout(null); // sets absolute layout
//frame.setPreferredSize(new Dimension();
frame.setPreferredSize(new Dimension(320, 500));
frame.setResizable(false);
okButton.setBounds(50, 400, 80, 30);
cancelButton.setBounds(180, 400, 80, 30);
treePane.setBounds(40, 80, 240, 260);
frame.add(okButton);
frame.add(cancelButton);
frame.add(treePane);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] argv)
{
NextPage frame = new NextPage();
return;
}
private void jbInit() throws Exception {
}
}
Moderator Edit: Code tags added