flow php

Example: Xml

Topic: Data reading


Description

Read data from a json file.

function from_xml(string|Path $path);

Additional options:

  • withXMLNodePath(string $xmlNodePath) - XML Node Path doesn’t support attributes, and it's not xpath, it is just a sequence of node names separated with slash
  • withBufferSize(int $size) - default 8096, the size of the buffer used to iterate through stream

Code

<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\XML\from_xml;
use function Flow\ETL\DSL\{data_frame, ref, to_stream};

require __DIR__ . '/../../../autoload.php';

data_frame()
    ->read(
        from_xml(__DIR__ . '/input/dataset.xml')
            ->withXMLNodePath('users/user')
    )
    ->withEntry('id', ref('node')->domElementAttributeValue('id'))
    ->withEntry('name', ref('node')->xpath('name')->domElementValue())
    ->withEntry('active', ref('node')->xpath('active')->domElementValue())
    ->withEntry('email', ref('node')->xpath('email')->domElementValue())
    ->drop('node')
    ->collect()
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+---------+--------+---------------------+
| id |    name | active |               email |
+----+---------+--------+---------------------+
|  1 |   Alice |   true |   [email protected] |
|  2 |     Bob |  false |     [email protected] |
|  3 | Charlie |   true | [email protected] |
|  4 |   David |  false |   [email protected] |
|  5 |    Emma |   true |    [email protected] |
|  6 |   Frank |  false |   [email protected] |
|  7 |   Grace |   true |   [email protected] |
|  8 |   Harry |  false |   [email protected] |
|  9 |    Isla |   true |    [email protected] |
| 10 |   James |  false |   [email protected] |
+----+---------+--------+---------------------+
10 rows

Contributors

Join us on GitHub external resource
scroll back to top