rfc:xmlreader_writer_streams

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
rfc:xmlreader_writer_streams [2024/04/24 19:30] – Update staticness nielsdosrfc:xmlreader_writer_streams [2024/05/10 16:10] (current) – Add example usages of the new APIs. nielsdos
Line 1: Line 1:
 ====== PHP RFC: Add openStream() to XML{Reader,Writer} ====== ====== PHP RFC: Add openStream() to XML{Reader,Writer} ======
-  * Version: 0.9.1+  * Version: 0.9.2
   * Date: 2024-04-21   * Date: 2024-04-21
   * Author: Niels Dossche <nielsdos@php.net>   * Author: Niels Dossche <nielsdos@php.net>
Line 46: Line 46:
  
 The third BC break is the promotion of the NULL-byte warning to a <php>ValueError</php>. This makes the <php>XMLReader</php> and <php>XMLWriter</php> class more consistent with other extensions that throw instead of issuing a warning. The migration for developers should be quite simple: instead of silencing the warning and/or checking the return value of the function, they should use a try-catch construct to handle the error. The third BC break is the promotion of the NULL-byte warning to a <php>ValueError</php>. This makes the <php>XMLReader</php> and <php>XMLWriter</php> class more consistent with other extensions that throw instead of issuing a warning. The migration for developers should be quite simple: instead of silencing the warning and/or checking the return value of the function, they should use a try-catch construct to handle the error.
 +
 +===== Example usages =====
 +
 +==== Minimal XMLReader example ====
 +
 +<PHP>
 +// Could be any stream, but this is for simplicity sake
 +$h = fopen("php://memory", "w+");
 +fwrite($h, "<root><!--my comment--><child/></root>");
 +fseek($h, 0);
 +
 +$reader = new XMLReader;
 +$reader->openStream($h);
 +
 +while ($reader->read()) {
 +    switch ($reader->nodeType) {
 +        case XMLReader::ELEMENT:
 +            echo "Element: ", $reader->name, "\n";
 +            break;
 +        case XMLReader::COMMENT:
 +            echo "Comment: ", $reader->value, "\n";
 +            break;
 +    }
 +}
 +</PHP>
 +
 +==== Minimal XMLWriter example ====
 +
 +<PHP>
 +// Could be any stream, but this is for simplicity sake
 +$h = fopen("php://output", "w");
 +
 +$writer = new XMLWriter;
 +$writer->openStream($h);
 +
 +$writer->startElement("root");
 +$writer->writeAttribute("align", "left");
 +$writer->writeComment("hello");
 +$writer->endElement();
 +$amount = $writer->flush();
 +echo "\nAmount of bytes written: ";
 +var_dump($amount);
 +</PHP>
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 95: Line 138:
 ===== Changelog ===== ===== Changelog =====
  
 +  - 0.9.2: Add example usages of the new APIs.
   - 0.9.1: Made XMLReader::openStream() non-static instead such that it works with overridden classes.   - 0.9.1: Made XMLReader::openStream() non-static instead such that it works with overridden classes.
   - 0.9: Initial version under discussion   - 0.9: Initial version under discussion
rfc/xmlreader_writer_streams.txt · Last modified: 2024/05/10 16:10 by nielsdos