jairo-parser
v0.0.6
Published
Simplified RDF/XML parser
Downloads
2
Readme
jairo-parser
Simplified RDF/XML parser
Jairo Parser is built on the powerful rdf parser library rdf-ext. You can use it inside Node.js application. Jairo Parser gives you a simpler way to parse RDF/XML by passing output into a simplified JSON object (Jairo Object ).
Installation
npm install jairo-parser
Usage
Parsing RDF/XML data from a string
var jairo = require('jairo-parser');
var prefixesOfInterest = [
{ 'prefix' : 'rdf' , 'uri' : 'http://www.w3.org/1999/02/22-rdf-syntax-ns##' },
{ 'prefix' : 'dcterms' , 'uri' : 'http://purl.org/dc/terms/' },
{ 'prefix' : 'foaf' , 'uri' : 'http://xmlns.com/foaf/0.1/' }
];
var rdf =
'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns##" ' +
' xmlns:dcterms="http://purl.org/dc/terms/" ' +
' xmlns:foaf="http://xmlns.com/foaf/0.1/"> ' +
' <rdf:Description rdf:about="http://example.org/123"> ' +
' <dcterms:creator> ' +
' <foaf:Person rdf:about="http://example.org/person45"> ' +
' <rdf:value>John Smith</rdf:value> ' +
' <foaf:phone rdf:resource="tel:+358-555-1234567"/> ' +
' </foaf:Person> ' +
' </dcterms:creator> ' +
' </rdf:Description> ' +
'</rdf:RDF>';
jairo.parse( 'rdfxml' , rdf , prefixesOfInterest , function( parsedData ){
console.log( parsedData );
jairo.printHumanReadbleResult( parsedData );
});
Parsing RDF/XML data from a file
File: test.rdf
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns##"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:foaf="http://xmlns.com/foaf/0.1/">
<rdf:Description rdf:about="http://example.org/123">
<dcterms:creator>
<foaf:Person rdf:about="http://example.org/person45">
<rdf:value>John Smith</rdf:value>
<foaf:phone rdf:resource="tel:+358-555-1234567"/>
</foaf:Person>
</dcterms:creator>
</rdf:Description>
</rdf:RDF>
Load RDF/XML data from a file
var jairo = require('jairo-parser');
var fs = require('fs');
var prefixesOfInterest = [
{ 'prefix' : 'rdf' , 'uri' : 'http://www.w3.org/1999/02/22-rdf-syntax-ns##' },
{ 'prefix' : 'dcterms' , 'uri' : 'http://purl.org/dc/terms/' },
{ 'prefix' : 'foaf' , 'uri' : 'http://xmlns.com/foaf/0.1/' }
];
fs.readFile( 'test.rdf', 'utf8', function (err, fileContent) {
if (err) throw err;
jairo.parse( 'rdfxml' , fileContent , prefixesOfInterest , function( parsedData ){
console.log( parsedData );
jairo.printHumanReadbleResult( parsedData );
});
});
Output of console.log( parsedData );
Output of jairo.printHumanReadbleResult( parsedData );
Iterating over Jairo objects
Based on the example code, you can iterate triples by following code.
jairo.parse( 'rdfxml' , rdf , prefixesOfInterest , function( parsedData ){
console.log( parsedData );
for (var i = 0; i < parsedData.triples.length; i++) {
// Get properties on each triple
};
for (var prop in parsedData.prefixesParsed ) {
// List all parsed prefixes
}
for (var prop in parsedData.prefixesParsedStat ) {
// Count number of prefixes
}
});
Access each triple
for (var i = 0; i < parsedData.triples.length; i++) {
var triple = parsedData.triples[i];
console.log('subject_uri=' + triple.subject_uri);
console.log('subject_qn=' + triple.subject_qn);
console.log('predicate_uri=' + triple.predicate_uri);
console.log('predicate_qn=' + triple.predicate_qn);
console.log('object_uri=' + triple.object_uri);
console.log('object_qn=' + triple.object_qn);
console.log('subject_uri=' + triple.subject_uri);
console.log('subject_uri=' + triple.subject_uri);
};
Output
subject_uri=<http://example.org/123>
subject_qn=123
predicate_uri=<http://purl.org/dc/terms/creator>
predicate_qn=dcterms:creator
object_uri=<http://example.org/person45>
object_qn=person45
subject_uri=<http://example.org/person45>
subject_qn=person45
predicate_uri=<http://www.w3.org/1999/02/22-rdf-syntax-ns##type>
predicate_qn=rdf:type
object_uri=<http://xmlns.com/foaf/0.1/Person>
object_qn=Person
subject_uri=<http://example.org/person45>
subject_qn=person45
predicate_uri=<http://www.w3.org/1999/02/22-rdf-syntax-ns##value>
predicate_qn=rdf:value
object_uri=John Smith
object_qn=John Smith
subject_uri=<http://example.org/person45>
subject_qn=person45
predicate_uri=<http://xmlns.com/foaf/0.1/phone>
predicate_qn=foaf:phone
object_uri=tel:+358-555-1234567
object_qn=tel:+358-555-1234567
List all parsed prefixes
for (var prop in parsedData.prefixesParsed ) {
console.log('prefix=' + prop + ' uri=' + parsedData.prefixesParsed[prop]);
}
Output
prefix=dcterms:creator uri=http://purl.org/dc/terms/
prefix=rdf:type uri=http://www.w3.org/1999/02/22-rdf-syntax-ns##
prefix=rdf:value uri=http://www.w3.org/1999/02/22-rdf-syntax-ns##
prefix=foaf:phone uri=http://xmlns.com/foaf/0.1/
Count number of prefixes
for (var prop in parsedData.prefixesParsedStat ) {
console.log('prefix=' + prop + ' count=' + parsedData.prefixesParsedStat[prop]);
}
Output
prefix=dcterms:creator count=1
prefix=rdf:type count=1
prefix=rdf:value count=1
prefix=foaf:phone count=1