phpday 2027 - Call for Papers

XMLReader

Introduction

The XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.

Encoding

It is important to note that internally, libxml uses the UTF-8 encoding and as such, the encoding of the retrieved contents will always be in UTF-8 encoding.

User Contributed Notes 1 note

up
12
Anonymous
2 years ago
Allright, I'll do it myself:

$xmlreader = XMLReader::open('xml_file.xml');
$indent = 0;
while ($xmlreader->read() !== FALSE)
{
    switch ($xmlreader->nodeType)
    {
        case XMLReader::ELEMENT:
            echo str_repeat("\t", ($xmlreader->isEmptyElement ? $indent : $indent++)), '[element]: ', $xmlreader->name, "\n";
            break;
        case XMLReader::TEXT:
            echo str_repeat("\t", $indent), $xmlreader->value, "\n";
            break;
        case XMLReader::END_ELEMENT:
            echo str_repeat("\t", --$indent), '[end element]: ', $xmlreader->name, "\n";
            break;
    }
}
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top