PHP libxml

Beginner
6 min read
Updated: Nov 2026
3 examples
XML parsing

Definition and Usage

libxml is the C library PHP uses to parse and write XML. You rarely call it directly: SimpleXML, DOM, XMLReader, and XMLWriter all sit on top of it.

The libxml_* functions control that backend. They decide how parse problems are reported (libxml_use_internal_errors(), libxml_get_errors()) and how external resources are loaded (libxml_set_external_entity_loader()).

This page explains the ideas. The libxml Functions index lists all eight function guides.

Errors vs warnings.

Without libxml_use_internal_errors(true), bad XML prints PHP warnings. With it, errors are stored quietly and you read them when you choose.

Untrusted XML.

Do not pass LIBXML_NOENT for input you do not control, and add LIBXML_NONET to block network fetches while parsing.

PHP Syntax

The usual pattern around any XML parse:

PHP
$previous = libxml_use_internal_errors(true);  // collect errors quietly

$doc = new DOMDocument();
$ok  = $doc->loadXML($xml, LIBXML_NONET);      // no network access

if (!$ok) {
    $errors = libxml_get_errors();             // array of LibXMLError
    libxml_clear_errors();                     // reset the buffer
}

libxml_use_internal_errors($previous);         // restore old mode

Core Ideas

What to remember when you parse XML in PHP:

IdeaExampleRemember
Backendsimplexml_load_string(), DOMDocumentBoth parse with libxml
Internal errorslibxml_use_internal_errors(true)Stores problems instead of warning
Read errorslibxml_get_errors()Array of LibXMLError objects
Clean uplibxml_clear_errors()Errors persist until cleared
Entity safetyLIBXML_NONETSkip LIBXML_NOENT on untrusted XML
Custom loadinglibxml_set_external_entity_loader()Decide which external files may load

PHP Examples

Three short scripts that show how libxml behaves.

Example 1 — Collect and clear errors

Parse broken XML without printing warnings, then reset the error list.

PHP
<?php
libxml_use_internal_errors(true);

$xml = simplexml_load_string('<root><item>One</root>');

echo 'parsed: ' . ($xml === false ? 'no' : 'yes') . "\n";
echo 'errors: ' . (count(libxml_get_errors()) > 0 ? 'yes' : 'no') . "\n";

libxml_clear_errors();
echo 'after clear: ' . count(libxml_get_errors()) . "\n";
?>

How It Works

  1. libxml_use_internal_errors(true) keeps warnings out of the output.
  2. simplexml_load_string() returns false on mismatched tags.
  3. libxml_clear_errors() empties the buffer for the next parse.

Example 2 — Read the last error

Check the severity of the most recent problem.

PHP
<?php
libxml_use_internal_errors(true);

$doc = new DOMDocument();
$ok  = $doc->loadXML('<a><b></a>');

$error = libxml_get_last_error();

echo 'loaded: ' . ($ok ? 'yes' : 'no') . "\n";
echo 'level: ' . ($error->level === LIBXML_ERR_FATAL ? 'fatal' : 'other') . "\n";
?>

How It Works

  1. loadXML() returns false for malformed XML.
  2. libxml_get_last_error() returns a LibXMLError or false.
  3. level is LIBXML_ERR_WARNING, LIBXML_ERR_ERROR, or LIBXML_ERR_FATAL.

Example 3 — Load XML safely

Block network access and avoid entity substitution on untrusted input.

PHP
<?php
$xml = '<?xml version="1.0"?><note><to>Ada</to></note>';

$doc = new DOMDocument();
$doc->loadXML($xml, LIBXML_NONET);

echo 'to: ' . $doc->getElementsByTagName('to')->item(0)->nodeValue . "\n";
?>

How It Works

  1. LIBXML_NONET stops libxml from fetching resources over the network.
  2. No LIBXML_NOENT means entities are not expanded into the document.
  3. libxml_disable_entity_loader() is deprecated; use these flags instead.

Next: PHP libxml Functions Index

After you know what libxml does, open the function index for all eight guides.

Start withWhy
libxml_use_internal_errors()Collect XML errors instead of warnings
libxml_get_errors()Read every stored error
libxml_clear_errors()Reset the error buffer
libxml_set_external_entity_loader()Control how external entities load

Full list: PHP libxml Functions index (/php/libxml/function).

Frequently Asked Questions

libxml is the C library PHP uses under the hood to parse and build XML. SimpleXML, DOM, XMLReader, XMLWriter, and XSL all rely on it, and the libxml_* functions let you control how it reports errors and loads external resources.
By default, malformed XML triggers PHP warnings that print to the page or log. Passing true stores problems inside libxml instead, so you can read them with libxml_get_errors() or libxml_get_last_error() and show a friendly message.
Yes, when you reuse the error buffer. Internal errors pile up across parses in the same request. Call libxml_clear_errors() after you handle them so the next document starts with a clean list.
No. It is deprecated since PHP 8.0 because libxml 2.9+ no longer loads external entities by default. Avoid the LIBXML_NOENT flag on untrusted input, and use LIBXML_NONET to block network access.
Practice the examples here, then open the libxml Functions index for all eight function guides, from libxml_use_internal_errors() to libxml_set_external_entity_loader().

Browse every libxml function

Open the functions index for categorized guides with examples and FAQs.

libxml Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful