0

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

Haroon Lone
  • 2,837
  • 5
  • 29
  • 65
  • It looks as if you are parsing for URLs (which point to models) not the models themselves. Hence the result is "empty". – Drux Aug 20 '15 at 11:28
  • @Drux But I think each triple in string should be considered as valid statement. Hence model should be able to read it. – Haroon Lone Aug 20 '15 at 11:33
  • I see, so those URLs are for resources. Which syntax is this meant to be. E.g. N3 would expect extra brackets and dots ... – Drux Aug 20 '15 at 11:36

2 Answers2

3

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());
}
jaco0646
  • 15,303
  • 7
  • 59
  • 83
0

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> ."
marstran
  • 26,413
  • 5
  • 61
  • 67