Home PHP libxml PHP libxml Definition
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.
Syntax
PHP Syntax The usual pattern around any XML parse:
$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 Reference
Core Ideas What to remember when you parse XML in PHP:
Idea Example Remember Backend simplexml_load_string(), DOMDocumentBoth parse with libxml Internal errors libxml_use_internal_errors(true)Stores problems instead of warning Read errors libxml_get_errors()Array of LibXMLError objects Clean up libxml_clear_errors()Errors persist until cleared Entity safety LIBXML_NONETSkip LIBXML_NOENT on untrusted XML Custom loading libxml_set_external_entity_loader()Decide which external files may load
Code
PHP Examples Three short scripts that show how libxml behaves.
1. Internal errors 2. Last error 3. Safe loading Example 1 — Collect and clear errors Parse broken XML without printing warnings, then reset the error list.
<?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";
?> parsed: no
errors: yes
after clear: 0 How It Works libxml_use_internal_errors(true) keeps warnings out of the output.simplexml_load_string() returns false on mismatched tags.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
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 loadXML() returns false for malformed XML.libxml_get_last_error() returns a LibXMLError or false.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
$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 LIBXML_NONET stops libxml from fetching resources over the network.No LIBXML_NOENT means entities are not expanded into the document.libxml_disable_entity_loader() is deprecated; use these flags instead.Continue
Next: PHP libxml Functions Index After you know what libxml does, open the function index for all eight guides.
Full list: PHP libxml Functions index (/php/libxml/function).
Frequently Asked Questions What is libxml in PHP? 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.
Why use libxml_use_internal_errors(true)? 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.
Do I need to call libxml_clear_errors()? 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.
Is libxml_disable_entity_loader() still needed? 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.
Where should I go next? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion