PHP code example of tcja / domdxmlparser
1. Go to this page and download the library: Download tcja/domdxmlparser library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tcja / domdxmlparser example snippets
// Since DOMDXMLParser uses method names such as sortBy() and toArray(),
// we use the class in its own namespace to avoid conflict with same methods
// names within famous frameworks like laravel or symfony
use Tcja\DOMDXMLParser;
$xml = new DOMDXMLParser('path/to/xml/file');
$check = $xml->checkNode('email', '[email protected] ');
var_dump($check); // Output : true if email was found, false if not
$check = $xml->checkNode('[email protected] ');
$data = $xml->pickNode('email', '[email protected] ')->fetchData()->toArray();
var_dump($data);
array(6) {
["id"]=>
string(1) "5"
["type"]=>
string(4) "user"
["email"]=>
string(20) "[email protected] "
["name"]=>
string(11) "Second User"
["password"]=>
string(60) "$2y$12$zXk23k.usjDgjbG6yJAKtO9EohFFAwnMOzsY3CZsKrgonz3/kh97a"
["nodeValue"]=>
string(12) "User account"
}
$data = $xml->pickNode('[email protected] ')->fetchData()->toArray();
$value = $xml->pickNode('email', '[email protected] ')->getAttr('name');
var_dump($value); // Output : "Second User";
$value = $xml->pickNode('[email protected] ')->getValue('name');
$value = $xml->pickNode('email', '[email protected] ')->getValue();
var_dump($value); // Output : "User account";
$value = $xml->pickNode('[email protected] ')->getValue('description');
$data = $xml->pickNode('account')->fetchData()->toArray();
var_dump($data);
array(6) {
[0]=>
array(6) {
["id"]=>
string(1) "1"
["type"]=>
string(13) "supreme admin"
["email"]=>
string(22) "[email protected] "
["name"]=>
string(13) "Supreme Admin"
["password"]=>
string(60) "$2y$12$BsNUcdEIoN2GeFLucqmfbuiGOWzHyCfxpOczswgMZXqrsmYZx363O"
["nodeValue"]=>
string(21) "Supreme admin account"
}
[1]=>
array(6) {
["id"]=>
string(1) "2"
["type"]=>
string(5) "admin"
["email"]=>
string(20) "[email protected] "
["name"]=>
string(11) "First Admin"
["password"]=>
string(60) "$2y$12$jZGZuXODSvBXTQXtvguHTOnXxdFgamQvWumSBYQ11bkCWR/tG5ZIu"
["nodeValue"]=>
string(13) "Admin account"
}
[2]=>
array(6) {
["id"]=>
string(1) "3"
["type"]=>
string(5) "admin"
["email"]=>
string(21) "[email protected] "
["name"]=>
string(12) "Second Admin"
["password"]=>
string(60) "$2y$12$pCPyVWZWAtXNLYBoCKTlY.pxZhEJEq6Rf8JV0eDsjo6sArkzTYyqi"
["nodeValue"]=>
string(13) "Admin account"
}
[3]=>
array(6) {
["id"]=>
string(1) "4"
["type"]=>
string(4) "user"
["email"]=>
string(19) "[email protected] "
["name"]=>
string(10) "First User"
["password"]=>
string(60) "$2y$12$ZoNNlEc9LjXjBynueyqljO5pai.ZVz4RjLZxMdLxBijwwRd5H70OS"
["nodeValue"]=>
string(12) "User account"
}
[4]=>
array(6) {
["id"]=>
string(1) "5"
["type"]=>
string(4) "user"
["email"]=>
string(20) "[email protected] "
["name"]=>
string(11) "Second User"
["password"]=>
string(60) "$2y$12$zXk23k.usjDgjbG6yJAKtO9EohFFAwnMOzsY3CZsKrgonz3/kh97a"
["nodeValue"]=>
string(12) "User account"
}
[5]=>
array(6) {
["id"]=>
string(1) "6"
["type"]=>
string(4) "user"
["email"]=>
string(19) "[email protected] "
["name"]=>
string(10) "Third User"
["password"]=>
string(60) "$2y$12$pGQw1CMCryJu5j0FcPVqiORo3/g.Fkv78TaDAcy5m68UO//L8XQYS"
["nodeValue"]=>
string(12) "User account"
}
}
$data = $xml->pickNode('account')->fetchData()->sortBy('email')->toArray();
var_dump($data);
array(6) {
[0]=>
array(6) {
["id"]=>
string(1) "2"
["type"]=>
string(5) "admin"
["email"]=>
string(20) "[email protected] "
["name"]=>
string(11) "First Admin"
["password"]=>
string(60) "$2y$12$jZGZuXODSvBXTQXtvguHTOnXxdFgamQvWumSBYQ11bkCWR/tG5ZIu"
["nodeValue"]=>
string(13) "Admin account"
}
[1]=>
array(6) {
["id"]=>
string(1) "4"
["type"]=>
string(4) "user"
["email"]=>
string(19) "[email protected] "
["name"]=>
string(10) "First User"
["password"]=>
string(60) "$2y$12$ZoNNlEc9LjXjBynueyqljO5pai.ZVz4RjLZxMdLxBijwwRd5H70OS"
["nodeValue"]=>
string(12) "User account"
}
[2]=>
array(6) {
["id"]=>
string(1) "3"
["type"]=>
string(5) "admin"
["email"]=>
string(21) "[email protected] "
["name"]=>
string(12) "Second Admin"
["password"]=>
string(60) "$2y$12$pCPyVWZWAtXNLYBoCKTlY.pxZhEJEq6Rf8JV0eDsjo6sArkzTYyqi"
["nodeValue"]=>
string(13) "Admin account"
}
[3]=>
array(6) {
["id"]=>
string(1) "5"
["type"]=>
string(4) "user"
["email"]=>
string(20) "[email protected] "
["name"]=>
string(11) "Second User"
["password"]=>
string(60) "$2y$12$zXk23k.usjDgjbG6yJAKtO9EohFFAwnMOzsY3CZsKrgonz3/kh97a"
["nodeValue"]=>
string(12) "User account"
}
[4]=>
array(6) {
["id"]=>
string(1) "1"
["type"]=>
string(13) "supreme admin"
["email"]=>
string(22) "[email protected] "
["name"]=>
string(13) "Supreme Admin"
["password"]=>
string(60) "$2y$12$BsNUcdEIoN2GeFLucqmfbuiGOWzHyCfxpOczswgMZXqrsmYZx363O"
["nodeValue"]=>
string(21) "Supreme admin account"
}
[5]=>
array(6) {
["id"]=>
string(1) "6"
["type"]=>
string(4) "user"
["email"]=>
string(19) "[email protected] "
["name"]=>
string(10) "Third User"
["password"]=>
string(60) "$2y$12$pGQw1CMCryJu5j0FcPVqiORo3/g.Fkv78TaDAcy5m68UO//L8XQYS"
["nodeValue"]=>
string(12) "User account"
}
}
$data = $xml->pickNode('account')->fetchData()->sortBy('email', 'DESC')->toArray();
$data = $xml->pickNode('account')->fetchData('email')->toArray();
var_dump($data);
array(6) {
[0]=>
string(22) "[email protected] "
[1]=>
string(20) "[email protected] "
[2]=>
string(21) "[email protected] "
[3]=>
string(19) "[email protected] "
[4]=>
string(20) "[email protected] "
[5]=>
string(19) "[email protected] "
}
$data = $xml->pickNode('type', 'admin')->fetchData()->toArray();
var_dump($data);
array(2) {
[0]=>
array(6) {
["id"]=>
string(1) "2"
["type"]=>
string(5) "admin"
["email"]=>
string(20) "[email protected] "
["name"]=>
string(11) "First Admin"
["password"]=>
string(60) "$2y$12$jZGZuXODSvBXTQXtvguHTOnXxdFgamQvWumSBYQ11bkCWR/tG5ZIu"
["nodeValue"]=>
string(13) "Admin account"
}
[1]=>
array(6) {
["id"]=>
string(1) "3"
["type"]=>
string(5) "admin"
["email"]=>
string(21) "[email protected] "
["name"]=>
string(12) "Second Admin"
["password"]=>
string(60) "$2y$12$pCPyVWZWAtXNLYBoCKTlY.pxZhEJEq6Rf8JV0eDsjo6sArkzTYyqi"
["nodeValue"]=>
string(13) "Admin account"
}
}
$checkPassword = $xml->pickNode('email', '[email protected] ')->compareTo('password', '$2y$12$zXk23k.usjDgjbG6yJAKtO9EohFFAwnMOzsY3CZsKrgonz3/kh97a');
vard_dump($checkPassword); // Output : true
$highest_user_id = $xml->pickNode('account')->getHighestValue('id');
vard_dump($highest_user_id); // Output : 6
$total_items = $xml->getTotalItems();
vard_dump($total_items); // Output : 6
$xml->addNode('account', [
'id' => 7,
'type' => 'user',
'email' => '[email protected] ',
'name' => 'Fourth User',
'password' => '$2y$12$WOjspiqT7ZuuMMPGCeZJjuU4hguSpEV9TQSfnjZudGgF9PYj7OAEa',
'CDATA' => 'User account' // this will create a CDATA node value, if you just want pure text, use 'textNode' => 'value' instead
]);
$xml->addNode('account', [
'id' => 7,
'type' => 'user',
'email' => '[email protected] ',
'name' => 'Fourth User',
'password' => '$2y$12$WOjspiqT7ZuuMMPGCeZJjuU4hguSpEV9TQSfnjZudGgF9PYj7OAEa',
'description' => 'User account'
], true);
$xml->setLayoutStyleNoAttributes(true);
$xml->addNode('account', [
'id' => 7,
'type' => 'user',
'email' => '[email protected] ',
'name' => 'Fourth User',
'password' => '$2y$12$WOjspiqT7ZuuMMPGCeZJjuU4hguSpEV9TQSfnjZudGgF9PYj7OAEa',
'description' => 'User account'
]);
$xml->pickNode('id', 3)->changeData([
'type' => 'user',
'email' => '[email protected] '
]);
$xml->pickNode('id', 3)->changeData('email', '[email protected] ');
$xml->pickNode('id', 3)->changeData('newAttribute', 'New value');
$xml->pickNode('id', 3)->changeData('newAttribute', false);
$xml->pickNode('account')->changeData(['name' => 'NAME RESET']);
$xml->pickNode('name', 'Third User')->setValue('New node value');
$xml->pickNode('name', 'Third User')->setTextValue('New node value');
$xml->pickNode('email', '[email protected] ')->remove();