PHP code example of josemmo / uxml

1. Go to this page and download the library: Download josemmo/uxml 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/ */

    

josemmo / uxml example snippets


use UXML\UXML;

$xml = \UXML\UXML::newInstance('RootTagName');

$domElement = new DOMElement('TagName');
$xml = \UXML\UXML::fromElement($domElement);

$source = <<<XML
<fruits>
    <fruit>Banana</fruit>
    <fruit>Apple</fruit>
    <fruit>Tomato</fruit>
</fruits>
XML;
$xml = \UXML\UXML::fromString($source);

$xml = \UXML\UXML::newInstance('Parent');
$child = $xml->add('Child');
echo $child; // <Child />
echo $xml;   // <Parent><Child /></Parent>

$child = $xml->add('Child', 'Hello World!');
echo $child; // <Child>Hello World!</Child>

$feed = \UXML\UXML::newInstance('feed', null, [
    'xmlns' => 'http://www.w3.org/2005/Atom'
]);
echo $feed; // <feed xmlns="http://www.w3.org/2005/Atom" />

$link = $feed->add('link', 'Wow!', [
    'href' => 'https://www.example.com'
]);
echo $link; // <link href="https://www.example.com">Wow!</link>

$xml = \UXML\UXML::newInstance('people');
$xml->add('person')->add('name', 'Jane Doe');
echo $xml; // <people>
           //     <person>
           //         <name>Jane Doe</name>
           //     </person>
           // </people>

$xml->asXML();

$xml = \UXML\UXML::newInstance('person');
$xml->add('name', 'Jane');
$xml->add('surname', 'Doe');
$xml->add('color', 'green', ['hex' => '#0f0']);

echo $xml->get('*[@hex]'); // <color hex="#0f0">green</color>
var_dump($xml->get('birthday')); // NULL

$xml = \UXML\UXML::fromString('<a><b>1</b><b>2</b><b>3</b></a>');
foreach ($xml->getAll('b') as $elem) {
    echo "Element says: " . $elem->asText() . "\n";
}

$source = <<<XML
<movie>
    <name>Inception</name>
    <year>2010</year>
    <director>
        <name>Christopher</name>
        <surname>Nolan</surname>
        <year>1970</year>
    </director>
</movie>
XML;
$xml = \UXML\UXML::fromString($source);

echo $xml->get('director/year'); // <year>1970</year>
echo $xml->get('director')->get('year'); // <year>1970</year>
echo $xml->get('year'); // <year>2010</year>
echo $xml->get('director')->get('//year'); // <year>2010</year>

$source = <<<XML
<project>
    <public>
        <name>Alpha</name>
    </public>
    <confidential>
        <budget>1,000,000 USD</budget>
    </confidential>
</project>
XML;
$xml = \UXML\UXML::fromString($source);
$xml->get('confidential')->remove();
echo $xml; // <project><public><name>Alpha</name></public></project>

$xml->add('TagName', null, [
    'xmlns' => 'https://example.com',
    'xmlns:abc' => 'urn:abc',
    'attribute' => 'value'
])->add('abc:Child', 'Name');
echo $xml; // <TagName xmlns="https://example.com"
           //  xmlns:abc="urn:abc"
           //  attribute="value">
           //     <abc:Child>Name</abc:Child>
           // </TagName>

$xml = \UXML\UXML::fromString('<a xmlns:ns="urn:abc"><ns:b /></a>');
echo $xml->get('ns:b'); // <ns:b />
echo $xml->get('abc:b'); // Is NULL as the prefix does not exist

echo $xml->get('{urn:abc}b'); // <ns:b />

$xml = \UXML\UXML::newInstance('Test');
$xml->element(); // Returns a [DOMElement] object