-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathread-write-simplexml.php
More file actions
44 lines (33 loc) · 1.09 KB
/
read-write-simplexml.php
File metadata and controls
44 lines (33 loc) · 1.09 KB
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
38
39
40
41
42
43
<?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
* SimpleXMLElement.
*/
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;
if ($isElement && $node->name === 'offer' && !$node->isEmptyElement) {
$node = new XMLReaderNode($node);
$elem = $node->getSimpleXMLElement();
$elem->is_active = 'true';
$writer->writeRaw($elem->asXML());
$reader->next();
$iterator->skipNextRead();
} else {
$iterator->write();
}
}
$writer->endDocument();