Open In App

How to parse HTML DOM with DOMDocument ?

What is DOM Parser?

DOM Parser is a JavaScript library that parses HTML or XML documents into their corresponding Document Object Model (DOM).



HTML is a markup language that describes the structure of web pages. The DOM is a tree representation of the document. In order to create a page, you need to parse the HTML code into its corresponding DOM using DOM Parser.

DOM Parser is a very useful tool for developers who want to manipulate HTML/XML documents. It allows them to easily extract information from the DOM tree.



How to Parse HTML DOM with DOM Document? 

We can parse a string containing HTML and access elements by following 2  different methods.

DOMParser.parseFromString(): 

The parseFromString() method parses a string containing either HTML or XML, returning an HTML Document or XML Document.

Syntax:

parseFromString(string, mimeType)

Parameters:

  1. text/xml
  2. text/html

The value in the text/xml will call the XML parser and text/html by the HTML parser.

Example 1:




<script>
  const one = new DOMParser();
  const site = "<h1>GeeksforGeeks</h1>";
  const title="<h3>Parse Html DOM with DOMDocument</h3>";
  const htmlString = "<p>Html parsed</p>";
  const final = one.parseFromString(site, "text/html");
  console.log(final.body.firstChild.textContent);
  const final1 = one.parseFromString(title, "text/html");
  console.log(final1.body.firstChild.textContent);
  const final2 = one.parseFromString(htmlString, "text/html");
  console.log(final2.body.firstChild.textContent);
</script>

Output :

 

Example 2: 

Document.getElementById(): It returns the element with the given ID. The function takes two arguments: the document object and the id string.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <script>
        function changeColor(newColor) {
              const elem = document.getElementById('one');
              elem.style.color = newColor;
        }
    </script>
</head>
  
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
    <h3>Parse HTML with DOM</h3>
    <p id="one">Color after button pressed</p>
    <P>Press button to turn pink</P>
    <button onclick="changeColor('pink');">
        Pink
    </button>
    <br />
    <P>Press button to turn blue </P>
    <button onclick="changeColor('blue');">
        Blue
    </button>
</body>
  
</html>

Output: 

 


Article Tags :