如何在 Node.js 中使用 rdflib.js 或 rdf-parser-rdfxml 解析 RDF/XML

问题描述 投票:0回答:1

我正在尝试读取 rdf/xml 文件并可能以 JSON 或 JavaScript 对象或任何其他格式解析它。我尝试搜索很多节点库,但找不到任何好的示例。所有这些都提供了一些没有适当文档的API(或者他们提供了但我没有正确理解)。这是我尝试过的。

var fs = require('fs'),
rdfParser = require('rdf-parser-rdfxml');

var res=rdfParser(__dirname+'/1.xml');
console.log(res);

这里是1.xml文件

<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>

我也以非常类似的方式尝试过 rdflib.js。

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]);
        }
    })
});

帮我一个人:)提前谢谢

javascript node.js
1个回答
1
投票

我发现 fs.readFile 返回 Object 而不是 String。修改后的来源在这里:

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.