Streaming RDF/XML parser
npm install rdfxml-streaming-parser


A fast, _streaming_ RDF/XML parser
that outputs RDFJS-compliant quads.
``bash`
$ yarn install rdfxml-streaming-parser
This package also works out-of-the-box in browsers via tools such as webpack and browserify.
`javascript`
import {RdfXmlParser} from "rdfxml-streaming-parser";
_or_
`javascript`
const RdfXmlParser = require("rdfxml-streaming-parser").RdfXmlParser;
RdfXmlParser is a Node Transform stream
that takes in chunks of RDF/XML data,
and outputs RDFJS-compliant quads.
It can be used to pipe streams to,
or you can write strings into the parser directly.
`javascript
const myParser = new RdfXmlParser();
fs.createReadStream('myfile.rdf')
.pipe(myParser)
.on('data', console.log)
.on('error', console.error)
.on('end', () => console.log('All triples were parsed!'));
`
`javascript
const myParser = new RdfXmlParser();
fs.createReadStream('myfile.rdf')
.pipe(myParser)
.on('data', console.log)
.on('version', console.log) // Log rdf:version attribute values
.on('error', console.error)
.on('end', () => console.log('All triples were parsed!'));
`
The error thrown for unsupported versions can be skipped
by setting parseUnsupportedVersions to true when constructing the parser.
`javascript
const myParser = new RdfXmlParser();
myParser
.on('data', console.log)
.on('error', console.error)
.on('end', () => console.log('All triples were parsed!'));
myParser.write('');
myParser.write(
xml:base="http://example.org/triples/">);
myParser.write();
myParser.write();
myParser.write();
myParser.write();`
myParser.end();
This parser implements the RDFJS Sink interface,
which makes it possible to alternatively parse streams using the import method.
`javascript
const myParser = new RdfXmlParser();
const myTextStream = fs.createReadStream('myfile.rdf');
myParser.import(myTextStream)
.on('data', console.log)
.on('error', console.error)
.on('end', () => console.log('All triples were parsed!'));
`
Optionally, the following parameters can be set in the RdfXmlParser constructor:
* dataFactory: A custom RDFJS DataFactory to construct terms and triples. _(Default: require('@rdfjs/data-model'))_baseIRI
* : An initial default base IRI. _(Default: '')_defaultGraph
* : The default graph for constructing quads. _(Default: defaultGraph())_strict
* : If the internal SAX parser should parse XML in strict mode, and error if it is invalid. _(Default: false)_trackPosition
* : If the internal position (line, column) should be tracked an emitted in error messages. _(Default: false)_allowDuplicateRdfIds
* : By default multiple occurrences of the same rdf:ID value are not allowed. By setting this option to true, this uniqueness check can be disabled. _(Default: false)_validateUri
* : By default, the parser validates each URI. _(Default: true)_iriValidationStrategy
* : Allows to customize the used IRI validation strategy using the IriValidationStrategy enumeration. IRI validation is handled by validate-iri.js. _(Default: IriValidationStrategy.Pragmatic)_parseUnsupportedVersions
* : If no error should be emitted on unsupported versions. _(Default: false)_version
* : The version that was supplied as a media type parameter. _(Default: undefined)_
`javascript``
new RdfXmlParser({
dataFactory: require('@rdfjs/data-model'),
baseIRI: 'http://example.org/',
defaultGraph: namedNode('http://example.org/graph'),
strict: true,
trackPosition: true,
allowDuplicateRdfIds: true,
validateUri: true,
parseUnsupportedVersions: false,
});
This code is released under the MIT license.