xml

Lightweight XML parser, DOM builder, serializer and query helper with entity escaping.

v1.0.0Data formatsbundledimport "xml";
Overview Installation Documentation Examples

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 &amp; 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
Packagexml
Version1.0.0 (follows the compiler)
Entry filexml.hphp
Sourcepackages/lib/xml ↗

Documentation

API Reference

Functions

xml_parse(string $xml): XmlNode

Parses an XML string into a root XmlNode.

xml_stringify(XmlNode $node, bool $pretty = false, int $indent = 2, bool $declaration = false): string

Serializes an XmlNode tree to an XML string.

xml_escape(string $str): string

Escapes &, <, >, ", and ' into XML entities (&amp;, &lt;, &gt;, &quot;, &apos;).

xml_unescape(string $str): string

Unescapes &amp;, &lt;, &gt;, &quot;, &apos;, decimal character references (&#65;), and hexadecimal character references (&#x41;).

---

Class XmlNode

Properties

Constructor

Builder & Attribute Methods

Query Methods

Serialization

Error Handling

All validation and parsing failures throw Exception with a descriptive xml: ... message including line and column numbers where applicable:

Examples

01Quick 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 &amp; 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.