I am trying to read an rdf/xml file and parse it possibly in JSON or JavaScript Object or any other format. I try to search a lot of node libraries but couldn't find any good example to do so. All the have provide some API without proper documentation( Or they gave but I didn't understand properly). Here is something I have tried.
var fs = require('fs'),
rdfParser = require('rdf-parser-rdfxml');
var res=rdfParser(__dirname+'/1.xml');
console.log(res);
here is 1.xml file
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:oneM2M="http://www.onem2m.org/ontology/Base_Ontology#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:cfso="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
<oneM2M:Thing rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#KETICF">
<cfso:hasSpecies>
<cfso:Species rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Type1"/>
</cfso:hasSpecies>
<cfso:hasSPName>
<cfso:SPName rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Maxfor"/>
</cfso:hasSPName>
<cfso:hasLocation>
<cfso:Location rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Buyeo"/>
</cfso:hasLocation>
<cfso:hasCropName>
<cfso:CropName rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#CharryTomato">
<cfso:hasSpecies rdf:resource="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#Type1"/>
</cfso:CropName>
</cfso:hasCropName>
<cfso:hasControlMode>
<cfso:ControlMode rdf:about="http://203.254.173.81:8080/ontologies/ConnectedFarmServiceOntology.owl#A"/>
</cfso:hasControlMode>
</oneM2M:Thing>
</rdf:RDF>
I have also tried with rdflib.js in pretty much similar way.
var fs = require('fs'),
$rdf = require('rdflib');
//var parser = new xml2js.Parser();
fs.readFile(__dirname + '/1.xml', function(err, data) {
// Fetch data via a regular AJAX call, load from a file, or pass in a literal
// string. In this example, it was loaded from 'https://fred.me/profile'
var store = $rdf.graph() // Init a new empty graph
var contentType = 'application/rdf+xml'
var baseUrl = ""
var parsedGraph = $rdf.parse(data, store, baseUrl, contentType);
$rdf.parse(data,function(triples){
for (var i in triples){
console.log(triples[i]);
}
})
});
Help me out someone :) Thanx in advance
I have found fs.readFile returns Object instead of String. So modified Source is Here:
var fs = require("fs");
var $rdf = require("rdflib");
var rdfData = fs.readFileSync(__dirname + "/1.xml").toString();
var store = $rdf.graph();
var contentType = "application/rdf+xml";
var baseUrl = "http://IoFTriples.com";
try {
$rdf.parse(rdfData, store, baseUrl, contentType);
var stms = store.statementsMatching(undefined, undefined, undefined);
for (var i = 0; i < stms.length; i++) {
var stm = stms[i];
console.log(stm); // the WebID of a friend
}
} catch (err) {
console.log(err);
}