PHP code example of lianhua / superxml

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

    

lianhua / superxml example snippets


$xml = new SuperXML("/path/to/xml/file");

$nodes = $xml->xpathQuery("/inventory/fruits/fruit"); // DOMNodeList with 'Apple' and 'Banana' nodes

$count = $xml->xpathEval("count(/inventory/fruits/fruit)"); // 2

$xml->addChild("/inventory/fruits", "fruit", "Kiwi");

$xml->remove("/inventory/vegetables/vegetable");

$xml->replaceValue("/inventory/vegetables/vegetable", "Potato");

$xml->setAttribute("/document/vegetables/vegetable[.='Carrot']", "growIn", "soil");

$xml->removeAttribute("/document/vegetables/vegetable[.='Carrot']", "growIn", "soil");

$xml = new SuperXML("/path/to/xml/file", false);
xml
<inventory>
    <fruits>
        <fruit>Apple</fruit>
        <fruit>Banana</fruit>
    </fruits>
    <vegetables>
        <vegetable>Carrot</vegetable>
    </vegetables>
</inventory>
xml
<inventory>
    <fruits>
        <fruit>Apple</fruit>
        <fruit>Banana</fruit>
        <fruit>Kiwi</fruit>
    </fruits>
    <vegetables>
        <vegetable>Carrot</vegetable>
    </vegetables>
</inventory>
xml
<inventory>
    <fruits>
        <fruit>Apple</fruit>
        <fruit>Banana</fruit>
    </fruits>
    <vegetables>

    </vegetables>
</inventory>
xml
<inventory>
    <fruits>
        <fruit>Apple</fruit>
        <fruit>Banana</fruit>
    </fruits>
    <vegetables>
        <vegetable>Potato</vegetable>
    </vegetables>
</inventory>
xml
<inventory>
    <fruits>
        <fruit>Apple</fruit>
        <fruit>Banana</fruit>
    </fruits>
    <vegetables>
        <vegetable growIn="soil">Carrot</vegetable>
    </vegetables>
</inventory>
xml
<inventory>
    <fruits>
        <fruit>Apple</fruit>
        <fruit>Banana</fruit>
    </fruits>
    <vegetables>
        <vegetable>Carrot</vegetable>
    </vegetables>
</inventory>