GithubHelp home page GithubHelp logo

tpluscode / rdfxml-streaming-parser.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rdfjs/rdfxml-streaming-parser.js

0.0 1.0 0.0 358 KB

Streaming RDF/XML parser

Home Page: https://www.rubensworks.net/blog/2019/03/13/streaming-rdf-parsers/

License: MIT License

TypeScript 99.29% JavaScript 0.71%

rdfxml-streaming-parser.js's Introduction

RDF/XML Streaming Parser

Build Status Coverage Status npm version Greenkeeper badge

A fast, streaming RDF/XML parser that outputs RDFJS-compliant quads.

Installation

$ yarn install rdfxml-streaming-parser

This package also works out-of-the-box in browsers via tools such as webpack and browserify.

Require

import {RdfXmlParser} from "rdfxml-streaming-parser";

or

const RdfXmlParser = require("rdfxml-streaming-parser").RdfXmlParser;

Usage

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.

Print all parsed triples from a file to the console

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!'));

Manually write strings to the parser

const myParser = new RdfXmlParser();

myParser
  .on('data', console.log)
  .on('error', console.error)
  .on('end', () => console.log('All triples were parsed!'));

myParser.write('<?xml version="1.0"?>');
myParser.write(`<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:ex="http://example.org/stuff/1.0/"
         xml:base="http://example.org/triples/">`);
myParser.write(`<rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar">`);
myParser.write(`<ex:prop />`);
myParser.write(`</rdf:Description>`);
myParser.write(`</rdf:RDF>`);
myParser.end();

Import streams

This parser implements the RDFJS Sink interface, which makes it possible to alternatively parse streams using the import method.

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!'));

Configuration

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 initital 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)
new RdfXmlParser({
  dataFactory: require('@rdfjs/data-model'),
  baseIRI: 'http://example.org/',
  defaultGraph: namedNode('http://example.org/graph'),
  strict: true,
  trackPosition: true,
  allowDuplicateRdfIds: true,
});

License

This software is written by Ruben Taelman.

This code is released under the MIT license.

rdfxml-streaming-parser.js's People

Contributors

rubensworks avatar greenkeeper[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.