class Document extends DOMDocument (View source)

Constants

XML

Used with Document::save method to save document in XML format

HTML

Used with Document::save method to save document in HTML format

JSON

Used with Document::save method to convert and save document in JSON format

SIMPLE

Used with Document::toArray method to convert document in a simple array

MINIMAL

Used with Document::toArray method to convert document in a minimal array

COMPLETE

Used with Document::toArray method to convert document in a array with all properties

Properties

protected array $levels Used with Document::reporting method or in extended classes

Methods

void
__construct(string $version = '1.0', string $encoding = 'UTF-8')

Create a Document instance

void
reporting()

Set level error for exception, set LIBXML_ERR_NONE (or 0 - zero) for disable exceptions.

void
fromArray(array $data)

Convert a array in node elements

string
toJson(bool $format = Document::MINIMAL, int $options = 0)

Convert DOM to JSON string

array
toArray(int $type = Document::SIMPLE)

Convert DOM to Array

string
__toString()

Magic method, return a well-formed XML string

void
getNamespaces(DOMElement $element = null)

Get namespace attributes from root element or specific element

mixed
loadXML(string $source, int $options = 0)

Load XML from a string

mixed
load(string $filename, int $options = 0)

Load XML from a file

mixed
loadHTML(string $source, int $options = 0)

Load HTML from a string

mixed
loadHTMLFile(string $filename, int $options = 0)

Load HTML from a file

query(string $selector, DOMNode $context = null)

Use query-selector like CSS, jQuery, querySelectorAll

first(string $selector, DOMNode $context = null)

Use query-selector like CSS, jQuery, querySelector

Details

void __construct(string $version = '1.0', string $encoding = 'UTF-8')

Create a Document instance

Parameters

string $version The version number of the document as part of the XML declaration
string $encoding The encoding of the document as part of the XML declaration

Return Value

void

void reporting()

Set level error for exception, set LIBXML_ERR_NONE (or 0 - zero) for disable exceptions.

For disable only warnings use like this $dom->reporting(LIBXML_ERR_FATAL, LIBXML_ERR_ERROR)

  • 0 - `LIBXML_ERR_NONE` - Disable errors
  • 1 - `LIBXML_ERR_WARNING` - Show warnings in DOM
  • 2 - `LIBXML_ERR_ERROR` - Show recoverable errors in DOM
  • 3 - `LIBXML_ERR_FATAL` - Show DOM fatal errors

Return Value

void

void fromArray(array $data)

Convert a array in node elements

Parameters

array $data

Return Value

void

Exceptions

DomException

string toJson(bool $format = Document::MINIMAL, int $options = 0)

Convert DOM to JSON string

Parameters

bool $format
int $options JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR. The behaviour of these constants is described in http://php.net/manual/en/json.constants.php

Return Value

string

array toArray(int $type = Document::SIMPLE)

Convert DOM to Array

Parameters

int $type

Return Value

array

Exceptions

DomException

string __toString()

Magic method, return a well-formed XML string

Example:


$xml = new Dom;

$xml->fromArray(array(
    'foo' => 'bar'
));

echo $xml;

Return Value

string

void getNamespaces(DOMElement $element = null)

Get namespace attributes from root element or specific element

Parameters

DOMElement $element

Return Value

void

mixed loadXML(string $source, int $options = 0)

Load XML from a string

Parameters

string $source
int $options

Return Value

mixed

Exceptions

DomException

mixed load(string $filename, int $options = 0)

Load XML from a file

Parameters

string $filename
int $options

Return Value

mixed

Exceptions

DomException

mixed loadHTML(string $source, int $options = 0)

Load HTML from a string

Parameters

string $source
int $options

Return Value

mixed

Exceptions

DomException

mixed loadHTMLFile(string $filename, int $options = 0)

Load HTML from a file

Parameters

string $filename
int $options

Return Value

mixed

Exceptions

DomException

DOMNodeList query(string $selector, DOMNode $context = null)

Use query-selector like CSS, jQuery, querySelectorAll

Parameters

string $selector
DOMNode $context

Return Value

DOMNodeList

DOMNode first(string $selector, DOMNode $context = null)

Use query-selector like CSS, jQuery, querySelector

Parameters

string $selector
DOMNode $context

Return Value

DOMNode