soloproyectos-php / dom-node
A library for managing DOM documents. This library is part of the SoloProyectos PHP API.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 724
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 1
Type:php-library
Requires
- php: >=5.5.0
- soloproyectos-php/array: ~1.0
- soloproyectos-php/css-parser: ~1.0
- soloproyectos-php/dom: ~2.0
- soloproyectos-php/text: ~1.0
This package is not auto-updated.
Last update: 2024-05-09 20:58:59 UTC
README
A library to manage XML documents in an easy and intuitive way.
Installation
This library uses composer as dependency manager. Just create a composer.json
file:
{ "require": { "php": ">=5.5.0", "soloproyectos-php/dom-node": "~1.0" } }
And install the dependencies
php composer.phar install
# or execute the following command if you have installed 'composer' globally
composer install
See the composer tutorial for more info:
https://getcomposer.org/doc/01-basic-usage.md
Methods
Create nodes from a given source:
DomNode::createFromElement($element)
: creates an instance from a DOMElement objectDomNode::createFromNode($node)
: creates an instance from a DomNode objectDomNode::createFromString($string)
: creates an instance from a string
Basic methods:
DomNode::document()
: gets the internal DOMDocument instanceDomNode::elements()
: gets internal DOM elementsDomNode::name()
: gets the node nameDomNode::parent()
: gets the parent node or anull
valueDomNode::root()
: gets the root nodeDomNode::query($cssSelectors)
: finds nodes using CSS selectorsDomNode::xpath($expression)
: finds nodes using XPath expressionsDomNode::remove()
: removes the node from the documentDomNode::clear()
: removes all child nodesDomNode::data($name, [$value])
: gets or sets arbitrary dataDomNode::append($string)
: appends inner XML textDomNode::prepend($string)
: prepends inner XML textDomNode::html([$string])
: gets or sets inner XML textDomNode::text([$string])
: gets or sets inner text
Attributes:
DomNode::attr($name, [$value])
: gets or sets an attributeDomNode::hasAttr($name)
: checks if a node has an attribute
CSS attributes:
DomNode::css($name, [$value])
: gets or sets a CSS attributeDomNode::hasCss($name)
: checks if a node has a CSS attribute
Classes:
DomNode::addClass($className)
: adds a class to the nodeDomNode::hasClass($className)
: checks if a node has a classDomNode::removeClass($className)
: removes a class from the node
Basic Examples
Create instances
Create a simple node:
// creates a simple node with two attributes and inner text $item = new DomNode("item", array("id" => 101, "title" => "Title 101"), "Inner text here..."); echo $item;
Create a complex node:
// in this case we use a callback function to add complex structures into the node $root = new DomNode("root", function ($target) { // adds three subnodes for ($i = 0; $i < 3; $i++) { $target->append(new DomNode("item", array("id" => $i, "title" => "Title $i"), "This is the item $i")); } // appends some XML code $target->append("<text>This text is added to the end.</text>"); // prepends some XML code $target->prepend("<text>This text is added to the beginning</text>"); }); echo $root;
Create instances from a given source:
// creates an instance from a string $xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>'); // creates an instance from a given DOMElement $doc = new DOMDocument("1.0", "UTF-8"); $doc->loadXML('<root><item id="101" /><item id="102" /><item id="103" /></root>'); $xml = DomNode::createFromElement($doc->documentElement);
Use the query
method
You can use the same query
function to retrieve either single or multiple nodes.
$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>'); // selects and prints all items $items = $xml->query("item"); foreach ($items as $item) { echo $item . "\n"; } // select and prints a single item $item = $xml->query("item[id = 102]"); echo $item;
Use the attr
, text
and html
methods:
$xml = DomNode::createFromString(file_get_contents("test.xml")); // prints books info $books = $xml->query("books item"); foreach ($books as $book) { echo "Title: " . $book->attr("title") . "\n"; echo "Author: " . $book->attr("author_id") . "\n"; echo "ISBN: " . $book->query("isbn")->text() . "\n"; echo "Available: " . $book->query("available")->text() . "\n"; echo "Description: " . $book->query("description")->text() . "\n"; echo "---\n"; } // gets the number of items echo "Number of items: " . count($books); // prints inner XML text $genres = $xml->query("genres"); echo $genres->html();
Use the attr
, text
and html
methods to change contents:
In the previous example we used attr
, text
and html
for getting contents. In this example we are use the same methods to change the document.
$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>'); // changes or adds attributes and inner texts $item = $xml->query("item[id = 102]"); $item->attr("id", 666); $item->attr("title", "Item 666"); $item->text("I'm an inner text"); echo $item; // changes inner contents $item = $xml->query("item[id = 103]"); $item->html('<subitem>I am a subitem</subitem>'); echo $item;
Use prepend
and append
methods:
$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>'); // appends contents $item = $xml->query("item[id = 102]"); $item->append('<subitem id="102.1" title="Subitem title">This text goes to the end...</subitem>'); echo $xml; // appends a DomNode object $item->append(new DomNode("subitem", array("id" => "102.1", "title" => "Subitem title"), "Some inner text here ...")); echo $xml; // appends a DomNode object and calls the `callback` function $item->prepend(new DomNode("subitem", array("id" => "102.2", "title" => "Subitem title"), function ($target) { $target->text("I'm the first child node ..."); })); echo $xml;
Use the remove
and clear
methods:
$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>'); // removes a single item $item = $xml->query("item[id = 103]"); $item->remove(); echo $xml; // removes a list of items $items = $xml->query("item:even"); $items->remove(); echo $xml; // removes all chid nodes $xml->clear(); echo $xml;
Chaining
You can concatenate multiple methods in the same line:
$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>'); // changes and prints the node in the same line echo $xml->query("item[id = 102]")->attr("title", "Item 102")->text("Some text...")->append("<subitem />");