質問

i'm reading RDF documents from an InputStream, as follows (scala syntax):

def foo(rdfData: InputStream, dialect: String = null) = {
  require(List("RDF/XML", "N-TRIPLE", "TURTLE", "TTL", "N3", "RDF/XML-ABBREV").contains(dialect) || dialect == null)
  val model: Model = ModelFactory.createDefaultModel
  model.read(rdfData, null, dialect)
  doSomethingWithTriplesIterator(model.listStatements())
}

now, some triples has typed literals, so i do something like:

val it = model.listStatements()
it.next.getObject.toString 

i get (sometimes) something like: "13"^^<http://www.w3.org/2001/XMLSchema#int> or "Hello world"@en_US when i'm only intrested at the value as strings, i.e. 13 & Hello world in the given example.

is there a way to get the "naked" values staight out of jena's statements? if so, how? thanks.

EDIT:

trying AndyS solution, using a triple taken from yago, i got:

scala> val model = ModelFactory.createDefaultModel
model: com.hp.hpl.jena.rdf.model.Model = <ModelCom   {} | >

scala> val is = new java.io.ByteArrayInputStream("""<http://yago-knowledge.org/resource/Mount_Cramer> <http://yago-knowledge.org/resource/hasLatitude> "44.01102^^http://yago-knowledge.org/resource/degrees" .""".getBytes("UTF-8"))
is: java.io.ByteArrayInputStream = java.io.ByteArrayInputStream@2b3c4f10

scala> model.read(is, null, "N-TRIPLE")
res0: com.hp.hpl.jena.rdf.model.Model = <ModelCom   {http://yago-knowledge.org/resource/Mount_Cramer @http://yago-knowledge.org/resource/hasLatitude "44.01102^^http://yago-knowledge.org/resource/degrees"} |  [http://yago-knowledge.org/resource/Mount_Cramer, http://yago-knowledge.org/resource/hasLatitude, "44.01102^^http://yago-knowledge.org/resource/degrees"]>

scala> val it = model.listStatements
it: com.hp.hpl.jena.rdf.model.StmtIterator = com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl@55ef662c

scala> val stmt = it.next
stmt: com.hp.hpl.jena.rdf.model.Statement = [http://yago-knowledge.org/resource/Mount_Cramer, http://yago-knowledge.org/resource/hasLatitude, "44.01102^^http://yago-knowledge.org/resource/degrees"]

scala> val obj = stmt.getObject
obj: com.hp.hpl.jena.rdf.model.RDFNode = 44.01102^^http://yago-knowledge.org/resource/degrees

scala> val ltrl = obj.asLiteral
ltrl: com.hp.hpl.jena.rdf.model.Literal = 44.01102^^http://yago-knowledge.org/resource/degrees

scala> ltrl.getLexicalForm
res1: String = 44.01102^^http://yago-knowledge.org/resource/degrees

so as you see, i'm not getting the desired output. what am i doing wrong?

役に立ちましたか?

解決

What you want is the lexical form. See Literal.getLexicalForm.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top