PHP code example of dmt-software / xml-parser

1. Go to this page and download the library: Download dmt-software/xml-parser 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/ */

    

dmt-software / xml-parser example snippets


use DMT\XmlParser\Parser;
use DMT\XmlParser\Source\StringParser;
use DMT\XmlParser\Tokenizer;
 
$xml = '<books>
    <book><title lang="en_US">A book title</title></book>
    <book><title lang="en_US">An other book title</title></book>
<books>';

$parser = new Parser(new Tokenizer(new StringParser($xml));
while ($node = $parser->parse()) {
    // iterates: node <books>, node <book>, node <title>, text-node "A book title", node <book> etc
}

while ($node = $parser->parse()) {
    if ($node->localName !== 'book') {
        continue;
    }
    $book = $parser->parseXml();
    break;
}
// book is "<book><title>A book title</title></book>"