PHPackages                             runopencode/sax - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. runopencode/sax

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

runopencode/sax
===============

XML SAX (JAVA like) parser

4.0.1(7mo ago)310.0k—2.9%11MITPHPCI failing

Since Jan 13Pushed 7mo ago3 watchersCompare

[ Source](https://github.com/RunOpenCode/sax)[ Packagist](https://packagist.org/packages/runopencode/sax)[ RSS](/packages/runopencode-sax/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (14)Versions (18)Used By (1)

Java like SAX XML parsing
=========================

[](#java-like-sax-xml-parsing)

[![Packagist](https://camo.githubusercontent.com/be583de074f9b6b6b7b31d757d7c7706370bf84d5f7ad27627687fc63deb5728/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f52756e4f70656e436f64652f7361782e737667)](https://packagist.org/packages/runopencode/sax)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/864d4de23f9f705fe0d54a802a0f52ce331757dac5509d44a44a45cae781d851/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f52756e4f70656e436f64652f7361782f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/RunOpenCode/sax/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/94fc2f03d16ee268e70febcb5e8bebb0d2d7f3934aadb3ecfc82ad7fb2082a12/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f52756e4f70656e436f64652f7361782f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/RunOpenCode/sax/?branch=master)[![Build Status](https://camo.githubusercontent.com/f90113526cabdb5688044d7ec97f4eac419b7bc5d0bc52c9197ddfc830ae81d5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f52756e4f70656e436f64652f7361782f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/RunOpenCode/sax/build-status/master)[![Build Status](https://camo.githubusercontent.com/10ecf3f1abf3f2f72d2388a2d9311832fb9b6f38fb8706a133f331e0835f45e4/68747470733a2f2f7472617669732d63692e6f72672f52756e4f70656e436f64652f7361782e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/RunOpenCode/sax)

[![SensioLabsInsight](https://camo.githubusercontent.com/1c3ff761a2382b319c8d285f1e6f06b565019aa32c14c587d9c98f0e258ef022/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f36363365653065652d303863312d346565322d396435612d3638383962303630373762652f6269672e706e67)](https://insight.sensiolabs.com/projects/663ee0ee-08c1-4ee2-9d5a-6889b06077be)

This library enables you to parse XML documents with SAX in Java style: instead of handling events by using these nasty functions and callbacks (see official PHP documentation example [here](http://php.net/manual/en/example.xml-structure.php)), you can just inherit provided abstract class `RunOpenCode\Sax\Handler\AbstractSaxHandler` and implement all of its abstract methods.

Major benefit of using this library is clean, human-readable code.

Example:

```
class MySaxHandler extends RunOpenCode\Sax\Handler\AbstractSaxHandler {
    // ... your implementation
}

$result = RunOpenCode\Sax\SaxParser::factory()->parse(new MySaxHandler(), $myXmlDocumentResource);

```

List of methods which you ought to implement:

- `onDocumentStart`: executed when parsing started of XML document.
- `onElementStart`: executed when parser stumbled upon new XML tag.
- `onElementData`: executed when parser stumbled upon CDATA of some XML tag.
- `onElementEnd`: executed when parser stumbled upon closed already opened XML tag.
- `onDocumentEnd`: executed when parsing of XML document is done.
- `onParseError`: executed when parsing error is triggered.
- `getResult`: executed at very end of parsing process where you should provide a invoker with parsing results.

Since common usage of Sax parser is to have a stack of currently working elements, there is a prototype implementation of that as well in class `RunOpenCode\Sax\Handler\AbstractStackedSaxHandler`. It extends `RunOpenCode\Sax\Handler\AbstractSaxHandler` and provides you with possibility to get current working element via `getCurrentElementName()` as well as with stack size via `getStackSize()`.

**Important notes**

- Due to underlying implementation of PHP XML parser, all tag names in relevant event calls are provided uppercase. Per example, if you have tag ``, in relevant event methods your check for tag name should be `if ($name === 'TAG')`.
- Event `onParseError` is due to unrecoverable parsing error, however, it is up to you and your use case weather you are going to trigger error continue with execution.
- Event `onElementData` will trigger even if you have blank spaces only between tags in XML document.

SaxParser and StreamAdapterInterface
====================================

[](#saxparser-and-streamadapterinterface)

`RunOpenCode\Sax\SaxParser` is provided as utility class which ought to ease up your usage of your SaxHandler implementation. SaxHandler uses `Psr\Http\Message\StreamInterface` implementation as source of XML document for parsing, however, StreamAdapters can help you to work with various XML document sources, such as:

- Resources (file resources or PHP native streams)
- DOMDocument
- SimpleXMLElement

If you need any other type of XML document source, you can provide it by implementing `RunOpenCode\Sax\Contract\StreamAdapterInterface`, and you can register it to `RunOpenCode\Sax\SaxParser` instance via `SaxParser::addStreamAdapter()` method call.

When you invoke `SaxParser::parse()`, before parsing, source of provided XML document will be checked against available adapters and converted to `Psr\Http\Message\StreamInterface` implementation.

This library recommends [guzzlehttp/psr7](https://github.com/guzzle/psr7) and uses it as default `StreamInterface` implementation, but you can use any other implementation that suits your need.

Changelog
---------

[](#changelog)

### February 21th, 2017.

[](#february-21th-2017)

- **BC break**: Changed api, no more callback, invocation of `parse()` method should return parsing result.

### February 10th, 2017.

[](#february-10th-2017)

- Dropped support for PHP 5.x
- Added PHPUnit 6.x as requirement
- Added lib exceptions

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance64

Regular maintenance activity

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 97.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~237 days

Recently: every ~711 days

Total

16

Last Release

220d ago

Major Versions

1.0.0 → 2.0.02017-02-10

2.0.0 → 3.0.02017-02-21

3.2.2 → 4.02024-03-28

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/410738?v=4)[Nikola Svitlica](/maintainers/TheCelavi)[@TheCelavi](https://github.com/TheCelavi)

---

Top Contributors

[![TheCelavi](https://avatars.githubusercontent.com/u/410738?v=4)](https://github.com/TheCelavi "TheCelavi (44 commits)")[![stefan1145](https://avatars.githubusercontent.com/u/110608398?v=4)](https://github.com/stefan1145 "stefan1145 (1 commits)")

---

Tags

xmlxml-parsersax

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/runopencode-sax/health.svg)

```
[![Health](https://phpackages.com/badges/runopencode-sax/health.svg)](https://phpackages.com/packages/runopencode-sax)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[sabre/xml

sabre/xml is an XML library that you may not hate.

52832.2M131](/packages/sabre-xml)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4910.9M36](/packages/goetas-webservices-xsd2php-runtime)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
