dmt-software/xml-parser

Iterate over the elements within a xml

v0.5.1 2023-03-01 08:41 UTC

This package is auto-updated.

Last update: 2024-04-29 11:01:33 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>"