On 11/11/13 12:24, Luciane Monteiro wrote:
I tested this code, it works! But I have 2 questions:
1- How can I get the comment in a specific language ?
See the Jena API operations on Literals.
2- You answered me 2 ways of doing that, is there any specific reason to
choose one or the other?
No - they amount to the same thing, just two routes to much the same code.
Thank you very much!
Andy
2013/11/10 Joshua TAYLOR <[email protected]>
On Sun, Nov 10, 2013 at 9:28 AM, Luciane Monteiro <[email protected]>
wrote:
Yes, I did this: ( Here response is a List<DBpediaResource> )
Model model = ModelFactory.createDefaultModel();
List<String> uriList = new ArrayList<String>();
for( DBpediaResource dbResource : response ) {
String uri = dbResource.getFullUri();
uriList.add(uri);
}
for( String uri : uriList ) {
Resource resource = model.createResource(uri);
StmtIterator stmts = resource.listProperties(RDFS.comment);
while( stmts.hasNext() ) {
Statement stmt = stmts.next();
RDFNode comment = stmt.getObject();
System.out.println("Comment: " + comment.toString());
}
}
But when I try to print the Comment it returns me nothing. Is there
anything
wrong?
When you say it "returns you nothing", do you mean that you don't get any
output at all, or that you're getting "Comment: null" (i.e., that
comment.toString() is returning null for some values)? At any rate, it
looks like you're doing a lot more work than you need to. When you read
the remote content (e.g., with model.read(...) or RDFDataMgr.read(model,
uri) as Andy mentioned) you get a model back. Then you can either ask the
model for statements directly, or you can get a resource for
http://dbpedia.org/resource/Google directly and then ask for properties
from it. E.g.:
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.ResourceFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDFS;
public class DBpediaExample {
final static String DBPEDIA_GOOGLE = "http://dbpedia.org/resource/Google
";
public static void main(String[] args) {
// Create a model and read the DBpedia content into it.
final Model model = ModelFactory.createDefaultModel().read( DBPEDIA_GOOGLE
);
// Create a resource not associate with any model, and ask the model
// for statements with it as a subject and rdfs:comment as the property.
// null (for the object) is a wildcard. You could also use a resource
// that is associated with a model (e.g., model.createResource(
dbpediaGoogle )).
final Resource google1 = ResourceFactory.createResource( DBPEDIA_GOOGLE
);
StmtIterator stmts1 = model.listStatements( google1, RDFS.comment,
(RDFNode) null );
showObjects( stmts1 );
// Create a resource based on the model. This one is based on the model,
// so when you ask for its properties, you're asking about statements in
// the same model.
final Resource google2 = model.getResource( DBPEDIA_GOOGLE );
showObjects( google2.listProperties( RDFS.comment ));
}
public static void showObjects( final StmtIterator it ) {
while ( it.hasNext() ) {
System.out.println( it.next().getObject() );
}
}
}
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/