DOM, XML, HTML
Learn how to select nodes with CSS selector in PHP, convert DOM into Array and Array into DOM.
Load HTML
Loads an HTML document from a string:
use Inphinit\Dom\Document;
$handle = new Document(Document::HTML);
$handle->load('Hello World!bar');
var_dump($handle->document());
var_dump($handle->root());
Load XML
Loads an XML document from a string:
use Inphinit\Dom\Document;
$handle = new Document(Document::XML);
$handle->load('contents baz ');
var_dump($handle->document());
var_dump($handle->root());
Load files
Load XML and HTML files
use Inphinit\Dom\Document;
// parse XML
$handle = new Document(Document::XML);
$handle->load('sample.xml', true);
// parse HTML
$handle = new Document(Document::HTML);
$handle->load('sample.html', true);
It is possible to load remote documents, but it is important to take some precautions and you need to define the context of the flows for the next appropriate load, according to the documentation: https://www.php.net/manual/en/function.libxml-set-streams-context.php.
use Inphinit\Dom\Document;
$opts = array(
'http' => array(
'user_agent' => 'PHP libxml agent',
)
);
$context = stream_context_create($opts);
libxml_set_streams_context($context);
// parse HTML
$handle = new Document(Document::HTML);
$handle->load('sample.html', true);
Serialize to an HTML or XML string
To convert the document into a string, use the Inphinit\Dom\Document::dump() method:
use Inphinit\Dom\Document;
// parse XML
$handle = new Document(Document::XML);
$handle->load('sample.xml', true);
echo $handle->dump();
Save to a file
Se precisar converter para uma string você pode o método Inphinit\Dom\Document::save($file)
use Inphinit\Dom\Document;
// parse XML
$handle = new Document(Document::XML);
$handle->load('sample.xml', true);
$handle->save('foobar.xml');
Change the serialization behavior
If you are using XML (e.g., new Document(Document::XML)), the Inphinit\Dom\Document::dump() and Inphinit\Dom\Document::save($file) methods may have their behavior modified according to libXML (which may vary depending on the version). Supported values:
-
LIBXML_NOEMPTYTAG: Expand empty tags (e.g.,<br/>to<br></br>) -
LIBXML_NOXMLDECL: Drop the XML declaration (PHP 8.3+ / Libxml >= 2.6.21)
use Inphinit\Dom\Document;
$handle = new Document(Document::XML);
$handle->setSaveOptions(LIBXML_NOEMPTYTAG|LIBXML_NOXMLDECL);
$handle->fromArray([...]);
$handle->dump($handle->root());
Selecionar elementos DOM
CSS selectors are typically more convenient than XPath. For this reason, the framework supports CSS-style selectors. See CSS Selector in PHP for details.
Array with DOM
The library supports bidirectional conversion between XML/HTML and arrays. See DOM with Arrays for details.
Methods
| Method | Description |
|---|---|
__construct(int $type = 0) |
Use new Document(Document::XML) to define the document as XML, and in the case of HTML use new Document(Document::HTML) |
setSeverityLevels(int $levels) |
Define the severity level of libxml, which will affect the behavior of possible exceptions during document parsing. See more details in DOM Errors. |
setLoadOptions(int $options) |
Define libXML options for load a document or string. |
setSaveOptions(int $options) |
Define libXML options for dump or save a document as file. |
document() |
Get original document. |
root() |
Get root element. |
selector() |
Returns Inphinit\Dom\Selector instance. |
getNamespaces(DOMElement $element) |
Get namespace attributes from root element or specific element. |
load(string $source, bool $file = false) |
Load string or file. See setLoadOptions() method. |
dump(DOMNode $node) |
Serializes the document as an XML/HTML string. |
save(string $file) |
Serializes the document as an XML/HTML file. |
fromArray(array $data) |
Convert Array to DOM. See more details in Array to HTML. |
toArray(int $format) |
Convert DOM to Array. See more details in DOM to Array. |