thekid/rfc — Issue #179: RFC 0179: Add stream support to XML parser
Author: thekid
Created: 2011-10-09 15:37:27+00:00
State: open
Labels: implemented, rfc

Scope of Change
====
The xml.parser.XMLParser class will support parsing streams.

Rationale
====
Not having to load file contents into memory completely before being
ablt to parse them.

Functionality
====
The `xml.parser.XMLParser::parse()` method will be changed to accept
either strings (as it does now) or `xml.parser.InputSource` objects.

Current API
----
This shows that before parsing XML, it needs to be loaded into a string.
This may be best for small files but will create memory problems when
the file size increases:

```php
parse($xml);
?>
```

New API
----
Here, the parsing is done while loading the file from the stream. Also,
because FileInputStream can easily be substituted for any other input
stream, it's more flexible.

```php
parse(new FileInputSource(new File('example.xml')));
?>
```

More examples:

```php
parse(new StringInputSource($xml));
$parser->parse(new FileInputSource(new File('example.xml')));
$parser->parse(new StreamInputSource(new FileInputStream(new File('example.xml'))));
?>
```

Security considerations
====
None.

Speed impact
====
Faster for the stream case.

Dependencies
====
None, the old behaviour of passing a string to `parse()` will be
kept.

Related documents
====
http://xp-framework.net/rfc/contrib/rfc0179.diff

Comments
====
friebe, Sun Feb 15 21:45:00 2009
----
Not sure whether it wouldn't be more elegant if the InputSource class
was an interface, and instead of an if/else on types in the constructor,
different implementations of it were shipped.

Examples:

```php
parse(new StringInputSource($xml));
$parser->parse(new FileInputSource(new File('example.xml')));
$parser->parse(new StreamInputSource(new FileInputStream(new File('example.xml'))));
?>
```

friebe, Wed May 20 09:50:24 2009
----
Decided in favor of the interface approach.