Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:52:42+00:00 2026-06-10T14:52:42+00:00

I’m writing an Application where a user can upload .rdf-Files and then execute SPARQL-Queries

  • 0

I’m writing an Application where a user can upload .rdf-Files and then execute SPARQL-Queries on it. Right now I am stuck on how to format the query’s result. For example: an ASK-Query outputs a boolean and a SELECT-Query returns a ResultSet. CONSTRUCT and DESCRIBE return a new RDF-Graph.
How do I get the result of the given query in a form to pass it to my view? In there I hope to print out a HTML-Table.

For a better understanding of my Problem I created this minimalistic code example:

import java.io.InputStream;

import com.hp.hpl.jena.*;

public class playground {

    public static void main(String[] args) {

      InputStream in = FileManager.get().open("vc-db-1.rdf");
      Model model = ModelFactory.createDefaultModel();
      model.read(in, null);

      String queryStringSelect = "SELECT ?subject ?predicate ?object WHERE { ?subject ?predicate ?object }";
      String queryStringAsk = "ASK WHERE { ?subject ?predicate ?object }";
      String queryStringDescribe = "DESCRIBE * WHERE { ?subject ?predicate ?object }";
      String queryStringConstruct = "CONSTRUCT { ?subject ?predicate ?object } WHERE { ?subject ?predicate ?object }";

      QueryExecution qe = QueryExecutionFactory.create(queryStringDescribe,
        model);
      Query q = QueryFactory.create(queryStringDescribe);

      int queryType = q.getQueryType();
      switch (queryType) {
      case Query.QueryTypeAsk:
          boolean b = qe.execAsk(); // Result that has to be formatted
          ResultSetFormatter.outputAsTSV(System.out, b);
          break;
      case Query.QueryTypeConstruct:
          model = qe.execConstruct(); // Result that has to be formatted
          model.write(System.out);
          break;
      case Query.QueryTypeDescribe:
          model = qe.execDescribe(); // Result that has to be formatted
          model.write(System.out);
          break;
      case Query.QueryTypeSelect:
          ResultSet results = qe.execSelect(); // Result that has to be
                 // formatted
          ResultSetFormatter.outputAsTSV(System.out, results);
          break;
      }

      model.close();
      qe.close();

    }

}

The mentionend .rdf file can be downloaded here: http://jena.apache.org/tutorials/sparql_data/vc-db-1.rdf

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:52:44+00:00Added an answer on June 10, 2026 at 2:52 pm

    I finally found an answer:

    I take a ByteArrayOutputStream and use the various model.write() functions do the rest. For the SELECT-Query I use a textual representation because the provided ResultSetFormatter.outputAsRDF() gives me way to much nonsense back. Anyways, here’s the relevant code

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ...
    int queryType = q.getQueryType();
    switch (queryType) {
    case Query.QueryTypeAsk:
        boolean b = qe.execAsk();
        ResultSetFormatter.outputAsRDF(baos, notation, b);
        retval = baos.toString();
        break;
    case Query.QueryTypeConstruct:
        model = qe.execConstruct();
        model.write(baos, notation);
        retval = baos.toString();
        break;
    case Query.QueryTypeDescribe:
        model = qe.execDescribe();
        model.write(baos, notation);
        retval = baos.toString();
        break;
    case Query.QueryTypeSelect:
        ResultSet results = qe.execSelect();
        ResultSetFormatter.out(baos, results);
        retval = baos.toString();
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to
I'm parsing an XML file, the creators of it stuck in a bunch social
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.