jena 해석 rdf, nt, ttl 형식 데이터

2222 단어 Java
import java.io.InputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;

public class test
{
	public static void main(String args[])
	{
		//String inputFileName = "E:\\Pattern Mining\\test.rdf";
		String inputFileName = "E:\\Pattern Mining\\test.nt";
		//String inputFileName = "E:\\Pattern Mining\\test.ttl";
		
		Model model = ModelFactory.createDefaultModel();

		InputStream in = FileManager.get().open(inputFileName);
		if (in == null) 
		{
			throw new IllegalArgumentException("File: " + inputFileName + " not found");
		}

		//model.read(in, "","RDF/XML");//                  
		model.read(in, "","N3");
		//model.read(in, "","TTL");

		// list the statements in the graph
		StmtIterator iter = model.listStatements();

		// print out the predicate, subject and object of each statement
		while (iter.hasNext()) 
		{
			Statement stmt = iter.nextStatement(); // get next statement
			//Resource subject = stmt.getSubject(); // get the subject
			//Property predicate = stmt.getPredicate(); // get the predicate
			//RDFNode object = stmt.getObject(); // get the object

			String subject = stmt.getSubject().toString(); // get the subject
			String predicate = stmt.getPredicate().toString(); // get the predicate
			RDFNode object = stmt.getObject(); // get the object
			

			System.out.print("   " + subject);	
			System.out.print("    " + predicate);		
			if (object instanceof Resource) 
			{
				System.out.print("    " + object);
			}
			else {// object is a literal
				System.out.print("   \"" + object.toString() + "\"");
			}
			System.out.println(" ."); 
		}
	}
}


 
  
 
 

좋은 웹페이지 즐겨찾기