How to tweak this javascript file to read in my da

2019-03-01 10:17发布

I'm using this Javascript RDF parser.

In the documentation it says to use it accordingly:

getRDFURL(url,func,followSeeAlso)
Downloads and parses RDF from a url. url is the url to recieve the RDF from, use the full url, not a relative one, or the base url will be wrong. func is a javascript function to call when the rdf has been processed.

In the file for the parser, I spied this empty variable:

    var baseURL='';

and I filled it up like so:

    var baseURL='http://localhost:8888/demo/StackOverflow-Europe.rdf';

In my index.html file I tried to call this parsing script in this way:

<!DOCTYPE html>
<meta charset='utf-8'>
<html>
  <head>
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <link rel='stylesheet' href='style.css'>
  </head>
  <body>
    <!--
    <script type='text/javascript' src='script.js'></script>
    -->

    <script type='text/javascript' src='parser.js'></script>

  </body>
</html>

but finally... nothing happened.

What am I doing wrong?

I guess that's not the right way to call javascript files? Is that it? Or maybe there's another reason.

I'm not so familiar with Javascript.

1条回答
够拽才男人
2楼-- · 2019-03-01 10:47

As Jeen Broekstra said in the comments, the line you use in your html file:

<script type='text/javascript' src='parser.js'></script>

has to only purpose to load the the rdf-parser library.

You can load it directly on the main server

<script src="http://www.jibbering.com/rdf-parser/parser.js"></script>

and use your own script.

You can use the official demo as a starter point:

<script type="text/javascript">
 function demo() {
     foafNS = "http://xmlns.com/foaf/0.1/";
     myRDF  = new RDF();
     myRDF.getRDFURL('/foaf2.rdf', callback);
     function callback() {
         alert("http://jibbering.com/foaf2.rdf contains the following triples\n\n" + myRDF.toNTriples())
         nm   = myRDF.Match(null, null, foafNS + "name", "Jim Ley")
         mbox = myRDF.getSingleObject(null, nm[0].subject, foafNS + "mbox", null)
         alert("The e-mail address of Jim Ley is " + mbox)
     }
  }
  document.write('<p>See demo using <a href="/foaf2.rdf">/foaf2.rdf</a> &nbsp; &nbsp; <button onclick="demo()">See Demo</button></p>')
</script>
  • new RDF() allows you to create an RDF utility object
  • getRDFURL('/foaf2.rdf', callback) loads the foaf2.rdf file and set the function callback as a callback, i.e. this function is called when the rdf file has been completely loaded.
  • myRDF.toNTriples() returns all RDF triples.
  • nm = myRDF.Match(null, null, foafNS + "name","Jim Ley") returns an array of triples that match the subject/predicate/object pattern.
  • mbox = myRDF.getSingleObject(null, nm[0].subject, foafNS + "mbox", null) returns the value of the object in the collection of triples that matches the subject/predicate/object pattern, or an empty string if not found.
查看更多
登录 后发表回答