Example of Parsing and Processing HTML/XML in PHP: Quick Guide

HTML (Hypertext Markup Language) and XML (eXtensible Markup Language) are widely used to structure and represent web data. Parsing involves breaking down these documents into a format that’s readable and usable for applications. So, while working with web data, it is essential to understand how to parse and process HTML and XML. And there are dozens of tools and libraries provided by PHP that enable developers to extract information, manipulate content, and integrate data seamlessly. In this article, we’ll explore the techniques and libraries PHP offers to parse and process HTML and XML data effectively.

PHP offers several ways to accomplish this, allowing developers to extract specific information and manipulate data according to their needs. Here we discuss a few:

Parsing HTML with PHP

Using DOMDocument and DOMXPath

PHP’s DOMDocument class provides a robust and standardized way to parse HTML documents. Combined with DOMXPath, it enables you to navigate and query the document easily.


// Load HTML content
$html = file_get_contents('example.html');
$doc = new DOMDocument();

// Create an XPath instance
$xpath = new DOMXPath($doc);

// Extract specific elements
$titles = $xpath->query('//h2');
foreach ($titles as $title) {
    echo $title->nodeValue . "\n";

In this example, loadHTML loads the HTML content into the DOMDocument instance, and DOMXPath allows you to perform XPath queries on the document.

Extracting Elements and Attributes

To access specific elements or attributes, use XPath expressions or methods provided by the DOMDocument class.


// Extract attribute values
$link = $doc->getElementsByTagName('a')->item(0);
$href = $link->getAttribute('href');

// Extract element content
$paragraphs = $doc->getElementsByTagName('p');
foreach ($paragraphs as $paragraph) {
    echo $paragraph->textContent . "\n";

This code demonstrates how to extract attribute values and element content using the DOMDocument methods.

Parsing XML with PHP

SimpleXML for Basic Parsing

For simple XML structures, SimpleXML is a convenient choice.

$xml = simplexml_load_file('data.xml');
echo "Name: " . $xml->name . "\n";
echo "Age: " . $xml->age . "\n";

Here, simplexml_load_file loads the XML file, and you can access XML elements and their content as properties of the SimpleXMLElement object.

DOMDocument for Complex XML Manipulation

For complex XML manipulation, use DOMDocument as shown earlier for HTML.

$xmlDoc = new DOMDocument();

// XPath queries for XML
$xpath = new DOMXPath($xmlDoc);
$names = $xpath->query('//person/name');

foreach ($names as $name) {
    echo $name->nodeValue . "\n";

In this example, the DOMDocument instance is loaded with XML content and DOMXPath is used to query and extract specific elements.

Processing HTML/XML Data

Modifying Content

Both DOMDocument and SimpleXML allow you to modify content.

// Modifying HTML
$element = $doc->createElement('div', 'New Content');

// Modifying XML with SimpleXML
$xml->name = 'John Doe';
$xml->age = 30;

These code snippets demonstrate how to modify content within HTML and XML documents.

Adding Elements and Attributes

You can add new elements and attributes to HTML and XML documents.

// Adding element in HTML
$newParagraph = $doc->createElement('p', 'New Paragraph');

// Adding attribute in XML
$newAttribute = $xmlDoc->createAttribute('gender');
$newAttribute->value = 'male';

This example illustrates how to add elements and attributes to HTML and XML documents.


PHP offers adaptable tools for parsing and processing HTML and XML data. Whether you’re pulling information, modifying content, or integrating data into your applications, PHP DOMDocument, DOMXPath, and SimpleXML provide the necessary capabilities. Start exploring these techniques, you’ll gain the skills to work efficiently with web data, creating dynamic and data-rich applications.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.