Download the PHP package kuria/dom without Composer
On this page you can find all versions of the php package kuria/dom. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dom
DOM ###
Wrappers around the PHP DOM classes that handle the common DOM extension pitfalls.
Features
- HTML documents
- encoding sniffing
- optional tidy support (automatically fix broken HTML)
- HTML fragments
- XML documents
- XML fragments
- XPath queries
- creating documents from scratch
- optional error suppression
- helper methods for common tasks, such as:
- querying multiple or a single node
- checking for containment
- removing a node
- removing all nodes from a list
- prepending a child node
- inserting a node after another node
- fetching
<head>
and<body>
elements (HTML) - fetching root elements (XML)
Requirements
- PHP 7.1+
Container methods
These methods are shared by both HTML and XML containers.
Loading documents
<library> <book name="Don Quixote" author="Miguel de Cervantes" /> <book name="Hamlet" author="William Shakespeare" /> <book name="Alice's Adventures in Wonderland" author="Lewis Carroll" /> </library> XML;
$dom = XmlDocument::fromString($xml);
foreach ($dom->query('/library/book') as $book) { /* @var \DOMElement $book / var_dump("{$book->getAttribute('name')} by {$book->getAttribute('author')}"); }
Output:
string(34) "Don Quixote by Miguel de Cervantes"
string(29) "Hamlet by William Shakespeare"
string(49) "Alice's Adventures in Wonderland by Lewis Carroll"
Creating a new document
<users> <user username="bob" access-token="123456"/> <user username="john" access-token="foobar"/> </users>
Handling XML namespaces in XPath queries
<lib:root xmlns:lib="http://example.com/"> <lib:book name="Don Quixote" author="Miguel de Cervantes" /> <lib:book name="Hamlet" author="William Shakespeare" /> <lib:book name="Alice's Adventures in Wonderland" author="Lewis Carroll" /> </lib:root> XML;
$dom = XmlDocument::fromString($xml);
// register namespace in XPath $dom->getXpath()->registerNamespace('lib', 'http://example.com/');
// query using the prefix foreach ($dom->query('//lib:book') as $book) { /* @var \DOMElement $book / var_dump($book->getAttribute('name')); }
Output:
string(11) "Don Quixote"
string(6) "Hamlet"
string(32) "Alice's Adventures in Wonderland"
XML fragments
Loading an existing fragment
$dom = XmlFragment::fromString('<fruits><fruit name="Apple" /><fruit name="Banana" /></fruits>');
foreach ($dom->query('/fruits/fruit') as $fruit) { /* @var \DOMElement $fruit / var_dump($fruit->getAttribute('name')); }
Output:
string(5) "Apple"
string(6) "Banana"
Creating a new fragment
// initialize empty fragment $dom = new XmlFragment(); $dom->loadEmpty(['formatOutput' => true]);
// add a new element $person = $dom->getDocument()->createElement('person'); $person->setAttribute('name', 'John Smith');
$dom->getRoot()->appendChild($person);
// save echo $dom->save();
Output:
<person name="John Smith"/>