简体   繁体   中英

Read strings into Jena Model

I have some Triples stored in a string like

String st =
<http://dbpedia.org/resource/53debf646ad3465872522651> <http://dbpedia.org/resource/end> <http://dbpedia.org/resource/1407106906391> . 
<http://dbpedia.org/resource/53debf676ad3465872522655> <http://dbpedia.org/resource/foi> <http://dbpedia.org/resource/SpatialThing> .

Now I am using JENA to read the same string as

           Model md= ModelFactory.createDefaultModel();
            InputStream in = IOUtils.toInputStream(st,"UTF-8");
            System.out.println(in.available());
            try{
                md.read(in, "N-TRIPLES");
            }catch(Exception e){
                e.printStackTrace();
            }
          System.out.println("model size:"+md.size());

I know that the string is available to InputStream, but model size is always printed as 0. So md. read md. read is not working properly. How should I debug it?

Update It throws exception as

org.apache.jena.riot.RiotException: [line: 1, col: 7 ] Element or attribute do not match QName production: QName::=(NCName':')?NCName

I think syntax is fine according to N-TRIPLES. Where is the issue? For debugging purpose I have placed a small program at link

Use the three-argument read() method and pass null as the second argument (base uri).

static String triples =
    "<http://dbpedia.org/resource/53debf646ad3465872522651> <http://dbpedia.org/resource/end> <http://dbpedia.org/resource/1407106906391> ." +
    "\n<http://dbpedia.org/resource/53debf676ad3465872522655> <http://dbpedia.org/resource/foi> <http://dbpedia.org/resource/SpatialThing> .";

public static void main(String[] args) throws IOException {
    Model model = ModelFactory.createDefaultModel()
        .read(IOUtils.toInputStream(triples, "UTF-8"), null, "N-TRIPLES");
    System.out.println("model size: " + model.size());
}

Seems like your RDF syntax is a bit off. If you are parsing N3 or Turtle, then try changing your string to this:

String st =
"<http://dbpedia.org/resource/53debf646ad3465872522651> <http://dbpedia.org/resource/end> <http://dbpedia.org/resource/1407106906391> .
<http://dbpedia.org/resource/53debf676ad3465872522655> <http://dbpedia.org/resource/foi> <http://dbpedia.org/resource/SpatialThing> ."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM