PHPackages                             sweetrdf/quick-rdf-io - 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. sweetrdf/quick-rdf-io

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

sweetrdf/quick-rdf-io
=====================

Collection of parser and serializers compatible with sweetrdf/rdfInterface

1.3.1(2mo ago)213.3k↓33.5%4MITPHPPHP &gt;=8.0CI passing

Since Jan 16Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/sweetrdf/quickRdfIo)[ Packagist](https://packagist.org/packages/sweetrdf/quick-rdf-io)[ Docs](https://github.com/sweetrdf/quickRdfIo)[ RSS](/packages/sweetrdf-quick-rdf-io/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (22)Versions (35)Used By (4)

quickRdfIo
==========

[](#quickrdfio)

[![Latest Stable Version](https://camo.githubusercontent.com/36a12ee1bb45d369df92d17b8cea6a013d336fe501fc1128ccdbfc4c4e9e39f0/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f717569636b2d7264662d696f2f762f737461626c65)](https://packagist.org/packages/sweetrdf/quick-rdf-io)[![Build status](https://github.com/sweetrdf/quickRdfIo/workflows/phpunit/badge.svg?branch=master)](https://github.com/sweetrdf/quickRdfIo/workflows/phpunit/badge.svg?branch=master)[![Coverage Status](https://camo.githubusercontent.com/0fbf9d4ef25831566c28c270106549028acd1a11b43da0b25d39f48e34ecb6aa/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73776565747264662f717569636b526466496f2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sweetrdf/quickRdfIo?branch=master)[![License](https://camo.githubusercontent.com/dc4ef72120d8ae006ad0d6cc17704d4f9bd5e8f2b42d657b9dacec30890e2656/68747470733a2f2f706f7365722e707567782e6f72672f73776565747264662f717569636b2d7264662d696f2f6c6963656e7365)](https://packagist.org/packages/sweetrdf/quick-rdf-io)

Collection of RDF parsers and serializers implementing the  interface.

Originally developed for the [quickRdf](https://github.com/sweetrdf/quickRdf) library.

Supported formats
-----------------

[](#supported-formats)

formatread/writeclassimplementationstreaming\[1\]rdf-xmlrwRdfXmlParser, RdfXmlSerializerownyesn-triplesrwNQuadsParser, NQuadsSerializerownyesn-triples\*rwNQuadsParser, NQuadsSerializerownyesn-quadsrwNQuadsParser, NQuadsSerializerownyesn-quads\*rwNQuadsParser, NQuadsSerializerownyesturtlerwTriGParser, TrigSerializerpietercolpaert/hardfyestrigrwTriGParser, TrigSerializerpietercolpaert/hardfyesJsonLDrwJsonLdParser, JsonLdSerializerml/json-ldnoJsonLD\[2\]wJsonLdStreamSerializerown\[3\]yes\[1\] A streaming parser/serializer doesn't materialize the whole dataset in memory which assures constant (and low) memory footprint. (this feature applies only to the parser/serializer - see the section on memory usage below)
\[2\] Use the `jsonld-stream` value for the `$format` parameter of the `\quickRdfIo\Util::serialize()` to use this serializer.
\[3\] Outputs data only in the extremely flattened Json-LD but works in a streaming mode.

Installation
------------

[](#installation)

- Obtain the [Composer](https://getcomposer.org)
- Run `composer require sweetrdf/quick-rdf-io`

Automatically generated documentation
-------------------------------------

[](#automatically-generated-documentation)

It's very incomplete but better than nothing.
[RdfInterface](https://github.com/sweetrdf/rdfInterface/) and [ml/json-ld](https://github.com/lanthaler/JsonLD) documentation is included.

Usage
-----

[](#usage)

Remark - there are calls to two other libraries in examples [sweetrdf/quick-rdf](https://github.com/sweetrdf/quickRdf) and [sweetrdf/term-templates](https://github.com/sweetrdf/termTemplates). You may install them with `composer require sweetrdf/quick-rdf` and `composer require sweetrdf/term-templates`.

### Basic parsing

[](#basic-parsing)

Just use `\quickRdfIo\Util::parse($input, $dataFactory, $format, $baseUri)`, where:

- `$input` can be (almost) "anything containing RDF" (an RDF string, a path to a file, an URL, an opened resource (result of `fopen()`), a PSR-7 response or a PSR-7 StreamInterface object).
- `$dataFactory` is an object implementing the `\rdfInterface\DataFactory` interface, e.g. `new \quickRdf\DataFactory()`.
- `$format` is an **optional** explicit RDF format indication for handling rare situtations when the format can't be autodetected. See the `src/quickRdfIo/Util.php::getParser()` source code to see a list of all accepted `$format` values.
- `$baseUri` is an **optional** baseURI value (for some kind of `$input` values it can be autodected).

```
include 'vendor/autoload.php';

// create a DataFactory - it's needed by all parsers
// (DataFactory implementation comes from other package, here sweetrdf/quick-rdf)
$dataFactory = new \quickRdf\DataFactory();

// parse a file
$iterator = \quickRdfIo\Util::parse('tests/files/quadsPositive.nq', $dataFactory);
foreach ($iterator as $i) echo "$i\n";
// parse a remote file (with format autodetection as github wrongly reports text/html)
$url = 'https://github.com/sweetrdf/quickRdfIo/raw/master/tests/files/spec2.10.rdf';
$iterator = \quickRdfIo\Util::parse($url, $dataFactory);
foreach ($iterator as $i) echo "$i\n";
// parse a PSR-7 response (format recognized from the response content-type header)
$url = 'https://www.w3.org/2000/10/rdf-tests/RDF-Model-Syntax_1.0/ms_7.2_1.rdf';
$client = new \GuzzleHttp\Client();
$request = new \GuzzleHttp\Psr7\Request('GET', $url);
$response = $client->send($request);
$iterator = \quickRdfIo\Util::parse($response, $dataFactory);
foreach ($iterator as $i) echo "$i\n";
// parse a string containing RDF with format autodetection
$rdf = file_get_contents('https://www.w3.org/2000/10/rdf-tests/RDF-Model-Syntax_1.0/ms_7.2_1.rdf');
$iterator = \quickRdfIo\Util::parse($rdf, $dataFactory);
foreach ($iterator as $i) echo "$i\n";
// parse an PHP stream
$stream = fopen('tests/files/quadsPositive.nq', 'r');
$iterator = \quickRdfIo\Util::parse($stream, $dataFactory);
fclose($stream);

// in most cases you will populate a Dataset with parsed triples/quads
// (note that a Dataset implementation comes from other package, e.g. sweetrdf/quick-rdf)
$dataset = new \quickRdf\Dataset();
$url = 'https://github.com/sweetrdf/quickRdfIo/raw/master/tests/files/spec2.10.rdf';
$dataset->add(\quickRdfIo\Util::parse($url, $dataFactory));
echo $dataset;
```

### Basic serialization

[](#basic-serialization)

Just use `\quickRdfIo\Util::serialize($data, $format, $output, $nmsp)`, where:

- `$data` is an object implementing the `\rdfInterface\QuadIterator` interface, e.g. a Dataset or an iterator returned by the parser.
- `$format` specifies an RDF serialization format, e.g. `turtle` or `ntriples`. See the `src/quickRdfIo/Util.php::getSeriazlier()` source code to see a list of all accepted `$format` values.
- `$output` is an **optional** parameter describing where the output should be written. If it's missing or null, output is returned as a string. If it's a string, it's treated as a path to open with `fopen($output, 'wb')`. If it's a stream resource or PSR-8 `StreamInterface` instance, the output is just written into it.
- `$nmsp` is an **optional** parameter used to pass desired RDF namespace aliases to the serializer. Note some formats like n-triples and n-quads don't support namespace aliases while in others (e.g. turtle) it's very common to use them.

```
include 'vendor/autoload.php';

$iterator = ...some \rdfInterface\QuadIterator, e.g. one from parsing examples...

// serialize to file in text/turtle format
\quickRdfIo\Util::serialize($iterator, 'turtle', 'myFile.ttl');
// serialize to string
echo \quickRdfIo\Util::serialize($iterator, 'turtle');

// use given namespace aliases when serializing to turtle
$nmsp = new \quickRdf\RdfNamespace();
$nmsp->add('http://purl.org/dc/terms/', 'dc');
$nmsp->add('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf');
echo \quickRdfIo\Util::serialize($iterator, 'turtle', null, $nmsp);
```

### Basic conversion

[](#basic-conversion)

```
include 'vendor/autoload.php';
// create a DataFactory - it's needed by all parsers
// (note that DataFactory implementation comes from other package, e.g. sweetrdf/quick-rdf)
$dataFactory = new \quickRdf\DataFactory();

// or any other example from the "Basic parsing" section above
$iterator = \quickRdfIo\Util::parse('tests/files/puzzle4d_100k.nt', $dataFactory);
// or any other example from the "Basic serialization" section above
\quickRdfIo\Util::serialize($iterator, 'rdf', 'output.rdf');
```

### Basic filtering without a Dataset

[](#basic-filtering-without-a-dataset)

It's worth noting that basic triples/quads filtering can be done in a memory efficient way without usage of a Dataset implementation.

Let's say we want to copy all triples with the `https://vocabs.acdh.oeaw.ac.at/schema#hasIdentifier` predicate from the `test/files/puzzle4d_100k.nt` n-triples file into a `ids.ttl` turtle file.

A typical approach would be to load data into a Dataset, filter them there and finally serialize the Dataset:

```
include 'vendor/autoload.php';
$dataFactory = new \quickRdf\DataFactory();
$t = microtime(true);

// parse input into a Dataset
$iterator = \quickRdfIo\Util::parse('tests/files/puzzle4d_100k.nt', $dataFactory);
$dataset = new \quickRdf\Dataset();
$dataset->add($iterator);

// filter out non-matching triples
$template = new \termTemplates\QuadTemplate(null, $dataFactory->namedNode('https://vocabs.acdh.oeaw.ac.at/schema#hasIdentifier'), null);
$dataset->deleteExcept($template);

// serialize
\quickRdfIo\Util::serialize($dataset, 'turtle', 'ids.ttl');

print_r([
    'time [s]' => microtime(true) - $t,
    'memory [MB]' => (int) (memory_get_peak_usage(true) / 1024 / 1024),
]);
// 4.4s, 125 MB of RAM
```

but it can be also done by using a "filtering generator" instead of the Dataset. With this approach we avoid materializing the whole dataset in memory which should both reduce memory footprint and speed things up a little:

```
include 'vendor/autoload.php';
$dataFactory = new \quickRdf\DataFactory();
$t = microtime(true);

// prepare input generator
$iterator = \quickRdfIo\Util::parse('tests/files/puzzle4d_100k.nt', $dataFactory);

// create a generator performing the filtering
$template = new \termTemplates\QuadTemplate(null, $dataFactory->namedNode('https://vocabs.acdh.oeaw.ac.at/schema#hasIdentifier'), null);
$filter = function($iter, $tmpl) {
    foreach ($iter as $quad) {
        if ($tmpl->equals($quad)) {
            yield $quad;
        }
    }
};
// wrap it into something implementing \rdfInterface\QuadIterator for types compatibility
$wrapper = new \rdfHelpers\GenericQuadIterator($filter($iterator, $template));

// serialize our filtering generator
\quickRdfIo\Util::serialize($wrapper, 'turtle', 'ids.ttl');

print_r([
    'time [s]' => microtime(true) - $t,
    'memory [MB]' => (int) (memory_get_peak_usage(true) / 1024 / 1024),
]);
// 2.7s, 51 MB of RAM
```

Results are better but the memory footprint is still surprisingly high. This is because of the DataFactory implementation we've used and performance optimizations it's applying (which admitedly in our scenario only slow things down). We can can optimize further by using as dumb as possible DataFactory implementation (for that we need another package - `sweetrdf/simple-rdf`):

```
include 'vendor/autoload.php';
$dataFactory = new \simpleRdf\DataFactory();
$t = microtime(true);

// prepare input generator
$iterator = \quickRdfIo\Util::parse('tests/files/puzzle4d_100k.nt', $dataFactory);

// create a generator performing the filtering
$template = new \termTemplates\QuadTemplate(null, $dataFactory->namedNode('https://vocabs.acdh.oeaw.ac.at/schema#hasIdentifier'), null);
$filter = function($iter, $tmpl) {
    foreach ($iter as $quad) {
        if ($tmpl->equals($quad)) {
            yield $quad;
        }
    }
};
// wrap it into something implementing \rdfInterface\QuadIterator for types compatibility
$wrapper = new \rdfHelpers\GenericQuadIterator($filter($iterator, $template));

// serialize our filtering generator
\quickRdfIo\Util::serialize($wrapper, 'turtle', 'ids.ttl');

print_r([
    'time [s]' => microtime(true) - $t,
    'memory [MB]' => (int) (memory_get_peak_usage(true) / 1024 / 1024),
]);
// 1.9s, 2 MB of RAM
```

As we can see the optimized implementation is 2.3 times faster and has 60 times lower memory footprint that a Dataset-based one.

Notes:

- Check the [sweetrdf/term-templates](https://github.com/sweetrdf/termTemplates) library for more classes allowing to easily match triples/quads fulfilling given conditions.
- This approach is not limited to filtering. Simple triples/quads modifications can be applied similar way (just adjust the "filtering generator" `foreach` loop body).

### Manual parser/serializer instantiation

[](#manual-parserserializer-instantiation)

It's of course possible to instantiate particular parser/serializer explicitly.

This is the only option to fine-tune parser/serializer configuration, e.g.:

- Create a strict n-triples parser ```
    $parser = new \quickRdfIo\NQuadsParser($dataFactory, true, \quickRdfIo\NQuadsParser::MODE_TRIPLES);
    ```
- Create a JsonLD serializer applying compacting with a context read from a given file and producing pretty-printed JSON: ```
    $serializer = new \quickRdfIo\JsonLdSerializer(
        'http://baseUri',
        \quickRdfIo\JsonLdSerializer::TRANSFORM_COMPACT,
        JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT,
        'context.jsonld'
    );
    ```

Be aware that parsing/serialization with the manually created parser/serializer instance requires a little more code.

Compare

```
include 'vendor/autoload.php';
$data = ...data read from somewhere...

// using \quickRdfIo\Util::serialize()
\quickRdfIo\Util::serialize($data, 'jsonld', 'output.jsonld');

// using manually instantiated serializer
$serializer = new \quickRdfIo\JsonLdSerializer();
$output = fopen('output.jsonld', 'w');
$serializer->serialize($data, $output);
fclose($output);
```

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance88

Actively maintained with recent releases

Popularity29

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 95.4% 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 ~67 days

Recently: every ~134 days

Total

29

Last Release

61d ago

Major Versions

0.11.1 → 1.0.0-RC12022-11-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1f662e3acb95efd9208cdcd2c97437db366044aea705985251760efcbd23070?d=identicon)[zozlak](/maintainers/zozlak)

---

Top Contributors

[![zozlak](https://avatars.githubusercontent.com/u/6503177?v=4)](https://github.com/zozlak "zozlak (125 commits)")[![k00ni](https://avatars.githubusercontent.com/u/381727?v=4)](https://github.com/k00ni "k00ni (6 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sweetrdf-quick-rdf-io/health.svg)

```
[![Health](https://phpackages.com/badges/sweetrdf-quick-rdf-io/health.svg)](https://phpackages.com/packages/sweetrdf-quick-rdf-io)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

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

PHPackages © 2026

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