DOM Model - HTML Parser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • broli85
    New Member
    • Apr 2006
    • 7

    DOM Model - HTML Parser

    HI ALL

    I would like to know if anyone knows how to traverse the document.body (or any other method) to get the contents of the current page. It must be general enough so that I can traverse any html page using the same algorithm, because this will be included in a Firefox extension.

    THANKS A LOT!
  • TastyWheat
    New Member
    • Nov 2006
    • 1

    #2
    If by "traverse" you mean visit each node then you can use "node.childNode s" to get the collection of child nodes for that node. Or once you're at a node you can use "node.nextSibli ng". If you don't care what order they come in (but I assume you do) you can use "document.getEl ementsByTagName ("*")". That should give you every single node on the page.

    If you just want to see the source then once you get the HTMLHtmlElement object you just use "node.innerHTML ". This will get you all the text between "<html>" and "</html>". The best part about that is it will show you any changes made by JavaScript. Using the browsers view source option only shows the document before JavaScript has it's way.

    Comment

    • trunalb
      New Member
      • Dec 2006
      • 1

      #3
      hi,
      even I am looking to parse a HTML DOM. By parse I mean I want to extract all the tags that DOM contains (div,span,body etc) and also their classes and ids. and then store them in a hash table. Has anyone ever done this before or have any clue on how its done?
      Any help is greatly appreciated.
      Thanks

      Comment

      • mltsy
        New Member
        • Aug 2007
        • 1

        #4
        Are you saying you would like to use javascript on a page to examine all the contents of the page after the DOM has been loaded? The childNodes collection would be the way to do that. If you mean you want to use some other language to parse a string or file containing an HTML document, that's a different story. That's actually what I'm looking to do, but not finding much help so far.

        Comment

        Working...