Skip to content

php_ref_libxml

The libxml extension provides a comprehensive set of functions and constants for working with XML data in PHP. It forms the foundation for other PHP XML extensions like SimpleXML, DOM (Document Object Model), and XSLT. Understanding libxml is helpful for advanced XML manipulation and error handling.

These functions allow you to control various aspects of XML parsing and manipulation, including error handling, document loading options, and accessing libxml library information.

The libxml extension is enabled by default in modern PHP installations. You typically do not need to install it separately.

If you compiled PHP from source or are using a minimal environment, you might need to ensure the libxml development package (e.g., libxml2-dev on Debian/Ubuntu, libxml2-devel on CentOS/Fedora) was available during compilation. For most users relying on pre-built PHP packages, this is handled automatically.

Here are some key functions provided by the libxml extension:

FunctionDescription
libxml_clear_errors()Clears the libxml error buffer. Useful when processing multiple documents or operations.
libxml_get_errors()Retrieves an array of LibXMLError objects, detailing errors encountered during the last XML operation.
libxml_get_last_error()Retrieves the last error encountered as a LibXMLError object.
libxml_set_streams_context()Sets the stream context for the next libxml document load or write operation. Useful for controlling network access or other stream behaviors.
libxml_use_internal_errors(bool $use_errors = false)Enables or disables the standard libxml error reporting. When set to true, errors are suppressed, and you must use libxml_get_errors() to check for issues.
libxml_disable_entity_loader(bool $disable = true)Disables or enables the loading of external entities. It’s crucial to disable this (true) when parsing untrusted XML data to prevent XML External Entity (XXE) attacks.

These constants are used as flags, often passed to functions like simplexml_load_file(), simplexml_load_string(), DOMDocument::load(), etc., to control parsing behavior.

ConstantDescription
LIBXML_COMPACTActivates small node allocation optimization. May improve performance on smaller documents.
LIBXML_DTDATTRSets default DTD attributes.
LIBXML_DTDLOADLoads an external subset (like DTD).
LIBXML_DTDVALIDValidates the document against its DTD.
LIBXML_NOBLANKSRemoves blank nodes (ignorable whitespace).
LIBXML_NOCDATAMerges CDATA sections into text nodes.
LIBXML_NOEMPTYTAGExpands empty tags (e.g., <tag/> to <tag></tag>). Affects saving with DOMDocument.
LIBXML_NOENTSubstitutes entities. Warning: Can be dangerous with untrusted XML due to entity expansion vulnerabilities. Consider LIBXML_NONET and libxml_disable_entity_loader().
LIBXML_NOERRORSuppresses error reports (use with libxml_use_internal_errors(true)).
LIBXML_NONETDisables network access during document loading. Important for security when dealing with potentially malicious external entities or schemas.
LIBXML_NOWARNINGSuppresses warning reports.
LIBXML_NOXMLDECLDrops the XML declaration (e.g., <?xml version='1.0'?>) when saving a document with DOMDocument.
LIBXML_NSCLEANRemoves redundant namespace declarations.
LIBXML_XINCLUDEImplements XInclude substitution.
LIBXML_PARSEHUGERelaxes hardcoded limits of the parser. Use with caution, as it might impact performance or security.
LIBXML_BIGLINESAllows storing line numbers greater than 65535 by reporting them differently.
LIBXML_ERR_ERRORRepresents recoverable errors.
LIBXML_ERR_FATALRepresents fatal errors.
LIBXML_ERR_NONERepresents no errors.
LIBXML_ERR_WARNINGRepresents simple warnings.
LIBXML_VERSIONThe underlying libxml library version as an integer (e.g., 20901 for 2.9.1).
LIBXML_DOTTED_VERSIONThe underlying libxml library version as a string (e.g., “2.9.1”).