dmt-software/xml-parser

Iterate over the elements within a xml

v0.6.1 2024-06-19 16:51 UTC

This package is auto-updated.

Last update: 2024-12-20 10:57:55 UTC


README

Install

composer require dmt-software/xml-parser

Usage

Iterate over all the elements within the xml.

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
}

Select an element and call parseXml to get the current element including its contents.

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