开发者

Convert String to model or Statement in jena using Java program?

开发者 https://www.devze.com 2023-02-26 00:38 出处:网络
I made a program for RDF by using jena in java... I have to return the result in string format.. and then in other function i have to get it as a string format and convert it to either model or statem

I made a program for RDF by using jena in java... I have to return the result in string format.. and then in other function i have to get it as a string format and convert it to either model or statement.... Is that possible... If so how to do that... could some one help me with a sample c开发者_如何学运维ode...

Thanks in advance


If the RDF you want to serialize is less than your complete model, then create a temporary memory model and copy into it the statements to want to write. Use Model.write to convert those statements to a string (in RDF/XML, Turtle or N-triples format). When you want to load a string containing RDF, create a java.io.StringReader object containing your string and pass that to the Model.read method.


It may be important to note that, according to the latest JavaDoc, the two Model.read() methods that take a Reader as a method parameter all say "Using this method is often a mistake.". I do not know why the JavaDoc says that, but it does. An alternative that I am using is to pass in an InputStream, as shown (where 'is' is the InputStream):

// read(InputStream in, String base, String lang)...

memModel.read(is, null,"TTL");

If you need to turn a String into an InputStream first, you can use:

InputStream is = new ByteArrayInputStream( str.getBytes() );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号