building an html dom tree, java?

???? mike lol

New member
ok so i parsed a simple html file into an arrayList of nodes. the node class is:
String s; //name of node
ArrayList<node> children;
public node(String) {
****this.s = s;
****children = null;
}

an example arrayList looks like:
[<html>, <body>, "oh", <em>, "hi", </em>, "there", </body>, </html>]

my DOMTree class is just:
node root;
public DOMTree(ArrayList<node> list) {
****root = buildTree(list);
}

and here's my buildTree, I cannot find out what's wrong with it:


//use stacks to build tree
public static node buildTree(ArrayList<node> list) {
****node currNode = null;
****Stack<node> s = new Stack<node>();

****//for every node in list
****for(int i = 0; i < list.size(); i++) {
********String name = list.get(i).toString();

********//closing tag node
********if(name.startsWith("</")) {
************currNode = s.pop(); //go to most recent tag
********}

********//tag node
********else if(name.startsWith("<")) {
************name = name.substring(1, name.length() - 1); //take off angle brackets
************if(currNode == null) { //if still at root node
****************root = new node(name);
****************currNode = root;
****************s.push(currNode); //push all tag nodes
************}
************else {
****************currNode.addChild(new node(name));
****************currNode = currNode.children.get(currNode.children.size() - 1); //go to last child
****************s.push(currNode);
************}
********}

********//text node
********else {
************currNode.addChild(new node(name));
********}
****}
****return currNode;
}

all the nodes in the arraylist are added, but in the wrong order
in the example arraylist at the top, my print function would output:
<html>
<body>
oh
<em>
hi
there
</em>
</body>
</html>

any ideas?
 
Back
Top