-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathread-write-cdata.php
More file actions
38 lines (28 loc) · 959 Bytes
/
read-write-cdata.php
File metadata and controls
38 lines (28 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
/*
* This file is part of the XMLReaderIterator package.
*
* Copyright (C) 2021, 2022 hakre <http://hakre.wordpress.com>
*
* Example: Write XML with XMLWriter while reading from XMLReader with
* XMLWriterIteration - with CDATA; insert a new child element, here with
* XMLWriter.
*/
require('xmlreader-iterators.php'); // require XMLReaderIterator library
$xmlInputFile = 'data/offers.xml';
$xmlOutputFile = 'php://output';
$reader = new XMLReader();
$reader->open($xmlInputFile);
$writer = new XMLWriter();
$writer->openUri($xmlOutputFile);
$iterator = new XMLWritingIteration($writer, $reader);
$writer->startDocument();
foreach ($iterator as $node) {
$isElement = $node->nodeType === XMLReader::ELEMENT;
$iterator->write();
if ($isElement && $node->name === 'offer' && !$node->isEmptyElement) {
$writer->writeRaw("\n ");
$writer->writeElement('is_active', 'true');
}
}
$writer->endDocument();