How to print out Tree Structure?

How to print html dom tree?

  • i have a DOM tree of a parsed html file. the tree is made of nodes which hold a string and an arraylist of children nodes. i'm stumped on how to print out the html code equivalent of the tree. for simplicity, the DOM tree doesn't have any complicated stuff, just a few tags and some text. for example: html ----body --------center ------------the ------------em ----------------quick ------------brown fox this tree should be printed like: <html> <body> <center> the <em> quick </em> brown fox </center> </body> </html> i can't get the tree to print out the closing tags correctly. any help?

  • Answer:

    When you say that the closing tags don't print correctly, do you mean that they don't print at all, or that they don't print in the order the reverse order to that in which they were opened, or what? If they don't print at all, that could be because you have not explicitly included them in your programme. The DOM is a collection of elements, not of tags. Each element (rather most elements) have both an opening tag and a closing tag. However in the DOM they are represented as a single element. Chapter 12 of Eloquent Javascript (available online at http://eloquentjavascript.net/chapter12.html#exercise1 ) provides an example of printing out the DOM which may help. To see the solution click on "show solution".

some guy named mike at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.