DOM (Document Object Model) allows us to dynamically access and manipulate the HTML data. All the text data from an HTML file can also be extracted using DOMParser. DOM parser returns an HTML/XML/SVG object. All the objects can be accessed using the [ ] operator in javascript.
The HTML DOM Tree of objects:
Steps to get all the text from an HTML document using DOMParser:
Step 1: Declare an instance of DOMParser.
const parser = new DOMParser();
Step 2: Parse the document using .parseFromString() function. It takes two arguments, the string to be parsed and the type of document.
const parsedDocument = parser.parseFromString( htmlInput, "text/html");
Step 3:Use doc.all element to access the whole HTML page, now get its root element which is stored at 0th index. We can also use getElementByID() to get content of a specific element.
var allText = parsedDocument.all[0].textContent;
Finally, we will use the textContent attribute of doc.all[0] to get the text from all HTML elements.
Example:
html
< title >This is the title</ title > < div > < span >Geeks for neveropen</ span > < p >Content to be parsed </ p > </ div > |
Output:
This is the title Geeks for neveropen Content to be parsed
Example :
html
< h2 > DomParser to get all HTML content </ h2 > < p > Click on the button Below to parse the HTML document </ p > <!-- Paragraph element to show the output --> < p id = "output" > </ p > <!-- Button to call the parsing function --> < button onclick = "printOutput()" > Parse now </ button > < script > // Input HTML string to be parsed var htmlInput = ` < title > This is the title </ title > < div > < span >Geeks for neveropen</ span > < p > Content to be parsed </ p > </ div > `; // Created instance const parser = new DOMParser(); // Parsing const parsedDocument = parser.parseFromString( htmlInput, "text/html"); // Getting text function printOutput() { var allText = parsedDocument .all[0].textContent; // Printing on page and console document.getElementById("output") .innerHTML = allText; console.log(parsedDocument .all[0].textContent); } </ script > |
Output:
The text content from individual components can also be retrieved using getElementsByClassName(‘className’) and getElementById(‘IDName’).
Javascript Function that takes the document to be parsed as a string and prints the result.
function parse(htmlInput) { // Creating Parser instance const parser = new DOMParser(); // Parsing the document using DOM Parser // and storing the returned HTML object in // a variable const parsedDocument = parser .parseFromString(htmlInput, "text/html"); // Retrieve all text content from DOM object var allText = parsedDocument.all[0].textContent; // Printing the output to webpage and console.log(parsedDocument.all[0].textContent); }