Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Stack <TagNode> stk = new Stack <TagNode>();
- TagNode temp;
- if(!sc.hasNext()){
- return;
- }
- while (sc.hasNext()){
- String next = sc.nextLine();
- int length = next.length();
- if(!next.contains("<")) { // for words in sentences and such
- temp=new TagNode (next, null, null);
- if(stk.peek().firstChild!=null) {
- stk.peek().sibling=temp;
- }else {
- stk.peek().firstChild=temp;
- }
- }
- else if(next.contains("/")) {
- temp = stk.pop();
- if(!stk.isEmpty()) {
- if(stk.peek().firstChild!=null) {
- stk.peek().sibling=temp;
- }else {
- stk.peek().firstChild=temp;
- }
- }else {root=temp;}
- }
- else if(next.contains("<")) { //opening tag
- stk.push(new TagNode (next.substring(1, length-1), null, null));
- }
- }
Add Comment
Please, Sign In to add comment