Libraries / xml
Overview
Lightweight, zero-dependency XML parser, DOM node builder, serializer, and query helper for HolyPHP with entity escaping/unescaping and clear mismatched-tag errors.
bundled — works offline
zero dependencies
dom
markup
Quick start
import "xml";
$doc = xml_parse('
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="101">
<title>HolyPHP & Friends</title>
<author>Ada</author>
</book>
</catalog>
');
$title = $doc->query("book/title");
println($title->getText()); // HolyPHP & Friends
$book = new XmlNode("book", ["id" => "102"]);
$book->addChild(new XmlNode("title", [], "Systems Programming"));
$doc->addChild($book);
println(xml_stringify($doc, true, 2, true));
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install xml
This adds the library to hphp.yaml, installs it into .hphp/packages/xml/ and pins the exact version in hphp.lock:
require:
xml: "^1.0"
Use it
import "xml";
xml is bundled with the compiler, so import "xml"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install xml # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install xml
Package xml
Version 1.0.0 (follows the compiler)
Entry file xml.hphp
Source packages/lib/xml ↗
Documentation
API Reference
Functions
xml_parse(string $xml): XmlNode
Parses an XML string into a root XmlNode.
Supports XML declarations (<?xml ... ?>), processing instructions, comments (<!-- ... -->), <!DOCTYPE ...>, CDATA sections (<![CDATA[ ... ]]>), self-closing tags, attributes (single or double quoted), and nested elements.
Automatically unescapes XML entities in attribute values and text content.
xml_stringify(XmlNode $node, bool $pretty = false, int $indent = 2, bool $declaration = false): string
Serializes an XmlNode tree to an XML string.
$pretty: When true, formats output with newlines and indentation.
$indent: Spaces per indentation level (must be >= 0).
$declaration: When true, prepends <?xml version="1.0" encoding="UTF-8"?>.
xml_escape(string $str): string
Escapes &, <, >, ", and ' into XML entities (&, <, >, ", ').
xml_unescape(string $str): string
Unescapes &, <, >, ", ', decimal character references (A), and hexadecimal character references (A).
---
Class XmlNode
Properties
public string $tag — Element tag name.
public array $attributes — Map of attribute name (string) to value (string).
public array $children — List of child XmlNode instances.
public string $text — Element text content.
Constructor
new XmlNode(string $tag, array $attributes = [], string $text = "", array $children = [])
Builder & Attribute Methods
setAttr(string $name, string $value): XmlNode — Sets an attribute and returns $this.
getAttr(string $name, $default = null) — Returns the attribute value or $default.
hasAttr(string $name): bool — Returns true if the attribute exists.
removeAttr(string $name): XmlNode — Removes an attribute if present and returns $this.
setText(string $text): XmlNode — Sets text content and returns $this.
getText(): string — Returns text content.
addChild(XmlNode $child): XmlNode — Appends a child XmlNode and returns $this.
Query Methods
find(string $tag) — Returns the first direct child with matching $tag, or null.
findAll(string $tag): array — Returns all direct children with matching $tag.
query(string $path) — Resolves a /-separated descendant path (supports * wildcard) and returns the first matching XmlNode or null.
queryAll(string $path): array — Resolves a /-separated descendant path and returns all matching XmlNodes.
Serialization
toString(bool $pretty = false, int $indent = 2): string — Shorthand for xml_stringify($this, $pretty, $indent, false).
Error Handling
All validation and parsing failures throw Exception with a descriptive xml: ... message including line and column numbers where applicable:
xml: empty XML input
xml: mismatched closing tag </bar>, expected </foo> at line 1, col 10
xml: unclosed tag <foo> starting at line 1, col 1
xml: unclosed comment starting at line 1, col 1
xml: unclosed CDATA section starting at line 1, col 7
xml: unclosed attribute value starting at line 1, col 10
xml: unexpected content after root element at line 1, col 12
xml: invalid tag name '123'
xml: query path cannot be empty
Examples
01 Quick start
Bundled with the compiler — import "xml"; works offline in any file, or add it to a project with hphp install xml.
import "xml";
$doc = xml_parse('
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="101">
<title>HolyPHP & Friends</title>
<author>Ada</author>
</book>
</catalog>
');
$title = $doc->query("book/title");
println($title->getText()); // HolyPHP & Friends
$book = new XmlNode("book", ["id" => "102"]);
$book->addChild(new XmlNode("title", [], "Systems Programming"));
$doc->addChild($book);
println(xml_stringify($doc, true, 2, true));
Try it hphp install xml
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_xml.hphp.