blob: f977083ce464ed624b11fb8d77e4cf77ffa66d0c [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Apache Jena - Jena RDF/XML Output How-To</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="/css/bootstrap-icons.css" rel="stylesheet" media="screen"><link rel="stylesheet" type="text/css" href="https://jena.apache.org/sass/jena.1b17c39a117e22b46db4c66f6395dc27c134a60377d87d2d5745b8600eb69722.css" integrity="sha256-GxfDmhF&#43;IrRttMZvY5XcJ8E0pgN32H0tV0W4YA62lyI=">
<link rel="shortcut icon" href="/images/favicon.ico" />
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary" role="navigation">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="/index.html">
<img class="logo-menu" src="/images/jena-logo/jena-logo-notext-small.png" alt="jena logo">Apache Jena</a>
</div>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li id="homepage" class="nav-item"><a class="nav-link" href="/index.html"><span class="bi-house"></span> Home</a></li>
<li id="download" class="nav-item"><a class="nav-link" href="/download/index.cgi"><span class="bi-download"></span> Download</a></li>
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal"></span> Learn <b class="caret"></b></a>
<ul class="dropdown-menu">
<li class="dropdown-header">Tutorials</li>
<li><a class="dropdown-item" href="/tutorials/index.html">Overview</a></li>
<li><a class="dropdown-item" href="/documentation/fuseki2/index.html">Fuseki Triplestore</a></li>
<li><a class="dropdown-item" href="/documentation/notes/index.html">How-To's</a></li>
<li><a class="dropdown-item" href="/documentation/query/manipulating_sparql_using_arq.html">Manipulating SPARQL using ARQ</a></li>
<li><a class="dropdown-item" href="/tutorials/rdf_api.html">RDF core API tutorial</a></li>
<li><a class="dropdown-item" href="/tutorials/sparql.html">SPARQL tutorial</a></li>
<li><a class="dropdown-item" href="/tutorials/using_jena_with_eclipse.html">Using Jena with Eclipse</a></li>
<li class="dropdown-divider"></li>
<li class="dropdown-header">References</li>
<li><a class="dropdown-item" href="/documentation/index.html">Overview</a></li>
<li><a class="dropdown-item" href="/documentation/query/index.html">ARQ (SPARQL)</a></li>
<li><a class="dropdown-item" href="/documentation/io/">RDF I/O</a></li>
<li><a class="dropdown-item" href="/documentation/assembler/index.html">Assembler</a></li>
<li><a class="dropdown-item" href="/documentation/tools/index.html">Command-line tools</a></li>
<li><a class="dropdown-item" href="/documentation/rdfs/">Data with RDFS Inferencing</a></li>
<li><a class="dropdown-item" href="/documentation/geosparql/index.html">GeoSPARQL</a></li>
<li><a class="dropdown-item" href="/documentation/inference/index.html">Inference API</a></li>
<li><a class="dropdown-item" href="/documentation/ontology/">Ontology API</a></li>
<li><a class="dropdown-item" href="/documentation/permissions/index.html">Permissions</a></li>
<li><a class="dropdown-item" href="/documentation/extras/querybuilder/index.html">Query Builder</a></li>
<li><a class="dropdown-item" href="/documentation/rdf/index.html">RDF API</a></li>
<li><a class="dropdown-item" href="/documentation/rdfconnection/">RDF Connection - SPARQL API</a></li>
<li><a class="dropdown-item" href="/documentation/rdfstar/index.html">RDF-star</a></li>
<li><a class="dropdown-item" href="/documentation/shacl/index.html">SHACL</a></li>
<li><a class="dropdown-item" href="/documentation/shex/index.html">ShEx</a></li>
<li><a class="dropdown-item" href="/documentation/tdb/index.html">TDB</a></li>
<li><a class="dropdown-item" href="/documentation/tdb2/index.html">TDB2</a></li>
<li><a class="dropdown-item" href="/documentation/query/text-query.html">Text Search</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-journal-code"></span> Javadoc <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/documentation/javadoc.html">All Javadoc</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/arq/">ARQ</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/fuseki2/">Fuseki</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/geosparql/">GeoSPARQL</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/jena/">Jena Core</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/permissions/">Permissions</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/extras/querybuilder/">Query Builder</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/shacl/">SHACL</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/tdb/">TDB</a></li>
<li><a class="dropdown-item" href="/documentation/javadoc/text/">Text Search</a></li>
</ul>
</li>
</ul>
<form class="d-flex" role="search" action="/search" method="GET">
<div class="input-group">
<input class="form-control border-end-0 border m-0" type="search" name="q" id="search-query" placeholder="Search...." aria-label="Search" style="width: 10rem;">
<button class="btn btn-outline-secondary border-start-0 border" type="submit">
<i class="bi-search"></i>
</button>
</div>
</form>
<ul class="navbar-nav">
<li id="ask" class="nav-item"><a class="nav-link" href="/help_and_support/index.html" title="Ask"><span class="bi-patch-question"></span><span class="text-body d-none d-xxl-inline"> Ask</span></a></li>
<li class="nav-item dropdown">
<a href="#" title="Get involved" class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-expanded="false"><span class="bi-megaphone"></span><span class="text-body d-none d-xxl-inline"> Get involved </span><b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/getting_involved/index.html">Contribute</a></li>
<li><a class="dropdown-item" href="/help_and_support/bugs_and_suggestions.html">Report a bug</a></li>
<li class="dropdown-divider"></li>
<li class="dropdown-header">Project</li>
<li><a class="dropdown-item" href="/about_jena/about.html">About Jena</a></li>
<li><a class="dropdown-item" href="/about_jena/architecture.html">Architecture</a></li>
<li><a class="dropdown-item" href="/about_jena/citing.html">Citing</a></li>
<li><a class="dropdown-item" href="/about_jena/team.html">Project team</a></li>
<li><a class="dropdown-item" href="/about_jena/contributions.html">Related projects</a></li>
<li><a class="dropdown-item" href="/about_jena/roadmap.html">Roadmap</a></li>
<li><a class="dropdown-item" href="/about_jena/security-advisories.html">Security Advisories</a></li>
<li class="dropdown-divider"></li>
<li class="dropdown-header">ASF</li>
<li><a class="dropdown-item" href="https://www.apache.org/">Apache Software Foundation</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/foundation/sponsorship.html">Become a Sponsor</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/licenses/LICENSE-2.0">License</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/security/">Security</a></li>
<li><a class="dropdown-item" href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
</ul>
</li>
<li class="nav-item" id="edit"><a class="nav-link" href="https://github.com/apache/jena-site/edit/main/source/documentation/io/rdfxml-output.md" title="Edit this page on GitHub"><span class="bi-pencil-square"></span><span class="text-body d-none d-xxl-inline"> Edit this page</span></a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="breadcrumbs">
<ol class="breadcrumb mt-4 p-2 bg-body-tertiary">
<li class="breadcrumb-item"><a href='/documentation'>DOCUMENTATION</a></li>
<li class="breadcrumb-item"><a href='/documentation/io'>IO</a></li>
<li class="breadcrumb-item active">RDFXML OUTPUT</li>
</ol>
</div>
<h1 class="title">Jena RDF/XML Output How-To</h1>
<main class="d-flex flex-xl-row flex-column">
<aside class="text-muted align-self-start mb-3 p-0 d-xl-none d-block">
<h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2>
<nav id="TableOfContents">
<ul>
<li><a href="#advanced-rdfxml-output">Advanced RDF/XML Output</a>
<ul>
<li><a href="#properties-to-control-rdfxml-output">Properties to Control RDF/XML Output</a></li>
</ul>
</li>
<li><a href="#conformance">Conformance</a></li>
<li><a href="#faster-rdfxml-io">Faster RDF/XML I/O</a></li>
</ul>
</nav>
</aside>
<article class="flex-column me-lg-4">
<h2 id="advanced-rdfxml-output">Advanced RDF/XML Output</h2>
<p>Two forms for output are provided: pretty printed RDF/XML (&ldquo;RDF/XML-ABBREV&rdquo;) or plain RDF/XML</p>
<p>While some of the code is shared, these
two writers are really very different, resulting in different but
equivalent RDF output. &ldquo;RDF/XML-ABBREV&rdquo; is slower, but should produce
more readable XML.</p>
<h3 id="properties-to-control-rdfxml-output">Properties to Control RDF/XML Output</h3>
<table>
<tr><th>Property Name</th><th>Description</th><th>Value class</th><th>Legal Values</th></tr>
<tr>
<td><tt>xmlbase</tt></td>
<td>The value to be included for an xml:base attribute on the root element in the file.</td>
<td><tt>String</tt></td>
<td>A URI string, or null (default)</td>
</tr>
<tr>
<td><tt>longId</tt></td>
<td>Whether to use long or short id's for anon resources. Short id's are easier to read and are the default, but can run out of memory on very large models.</td>
<td><tt>String</tt> or <tt>Boolean</tt></td>
<td><tt>"true"</tt>, <tt>"false"</tt> (default)</td>
</tr>
<tr>
<td><tt>allowBadURIs</tt></td>
<td>URIs in the graph are, by default, checked prior to serialization.</td>
<td><tt>String</tt> or <tt>Boolean</tt></td>
<td><tt>"true"</tt>, <tt>"false"</tt> (default)</td>
</tr>
<tr>
<td><tt>relativeURIs</tt></td>
<td>What sort of relative URIs should be used. A comma separated list of options:
<ul>
<li><em>same-document</em><br />
same-document references (e.g. &quot;&quot; or &ldquo;#foo&rdquo;)</li>
<li><em>network</em><br />
network paths e.g. <code>&quot;//example.org/foo&quot;</code> omitting the URI scheme</li>
<li><em>absolute</em><br />
absolute paths e.g. <code>&quot;/foo&quot;</code> omitting the scheme and authority</li>
<li><em>relative</em><br />
relative path not beginning in <code>&quot;../&quot;</code></li>
<li><em>parent</em><br />
relative path beginning in <code>&quot;../&quot;</code></li>
<li><em>grandparent</em><br />
relative path beginning in <code>&quot;../../&quot;</code></li>
</ul>
<p>The default value is &ldquo;same-document, absolute, relative, parent&rdquo;.
To switch off relative URIs use the value &ldquo;&rdquo;. Relative URIs of any
of these types are output where possible if and only if the option
has been specified.</td></p>
<td>String</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><tt>showXmlDeclaration</tt></td>
<td>
If true, an XML Declaration is included in the output, if false no XML declaration is included.
The default behaviour only gives an XML Declaration when asked to write to an `OutputStreamWriter`
that uses some encoding other than UTF-8 or UTF-16. In this case the encoding is shown in the
XML declaration. To ensure that the encoding attribute is shown in the XML declaration either:
<ul>
<li>Set this option to true and use the
<code>write(Model,Writer,String)</code> variant with an appropriate <code>OutputStreamWriter</code>.</li>
<li>Or set this option to false, and write the declaration to an <code>OutputStream</code> before calling
<code>write(Model,OutputStream,String)</code>.</li>
</ul>
</td>
<td><tt>true</tt>, <tt>"true"</tt>, <tt>false</tt>, <tt>"false"</tt> or <tt>"default"</tt></td>
<td>can be true, false or "default" (null)</td>
</tr>
<tr>
<td><tt>showDoctypeDeclaration</tt></td>
<td>
If true, an XML Doctype declaration is included in the output. This
declaration includes a `!ENTITY` declaration for each prefix mapping
in the model, and any attribute value that starts with the URI of
that mapping is written as starting with the corresponding entity
invocation.
</td>
<td><tt>String</tt> or <tt>Boolean</tt></td>
<td><tt>true</tt>, <tt>false</tt>, <tt>"true"</tt>, <tt>"false"</tt></td>
</tr>
<tr>
<td><tt>tab</tt></td>
<td>The number of spaces with which to indent XML child elements.</td>
<td><tt>String</tt> or <tt>Integer</tt></td>
<td>positive integer "2" is the default</td>
</tr>
<tr>
<td><tt>attributeQuoteChar</tt></td>
<td>How to write XML attributes.</td>
<td><tt>String</tt></td>
<td><tt>"\""</tt> or <tt>"'"</tt></td>
</tr>
<tr>
<td><tt>blockRules</tt></td>
<td>
A list of `Resource` or a `String` being a comma separated list of
fragment IDs from [http://www.w3.org/TR/rdf-syntax-grammar](http://www.w3.org/TR/rdf-syntax-grammar)
indicating grammar rules that will not be used. Rules that can be blocked are:
<ul>
<li><a href="http://www.w3.org/TR/rdf-syntax-grammar#section-Reification">section-Reification</a>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/RDFSyntax.html#sectionReification"><code>RDFSyntax.sectionReification</code></a>)</li>
<li><a href="http://www.w3.org/TR/rdf-syntax-grammar#section-List-Expand">section-List-Expand</a>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/RDFSyntax.html#sectionListExpand"><code>RDFSyntax.sectionListExpand</code></a>)</li>
<li><a href="http://www.w3.org/TR/rdf-syntax-grammar#parseTypeLiteralPropertyElt">parseTypeLiteralPropertyElt</a>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/RDFSyntax.html#parseTypeLiteralPropertyElt"><code>RDFSyntax.parseTypeLiteralPropertyElt</code></a>)</li>
<li><a href="http://www.w3.org/TR/rdf-syntax-grammar#parseTypeResourcePropertyElt">parseTypeResourcePropertyElt</a>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/RDFSyntax.html#parseTypeLiteralPropertyElt"><code>RDFSyntax.parseTypeLiteralPropertyElt</code></a>)</li>
<li><a href="http://www.w3.org/TR/rdf-syntax-grammar#parseTypeCollectionPropertyElt">parseTypeCollectionPropertyElt</a>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/RDFSyntax.html#parseTypeCollectionPropertyElt"><code>RDFSyntax.parseTypeCollectionPropertyElt</code></a>)</li>
<li><a href="http://www.w3.org/TR/rdf-syntax-grammar#idAttr">idAttr</a>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/RDFSyntax.html#idAttr"><code>RDFSyntax.idAttr</code></a>)</li>
<li><a href="http://www.w3.org/TR/rdf-syntax-grammar#propertyAttr">propertyAttr</a>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/RDFSyntax.html#propertyAttr"><code>RDFSyntax.propertyAttr</code></a>)</li>
</ul>
<p>In addition <code>&quot;daml:collection&quot;</code>
(<a href="/documentation/javadoc/jena/org.apache.jena.core/org/apache/jena/vocabulary/DAML_OIL.html#collection"><code>DAML_OIL.collection</code></a>)
can be blocked. Blocking
<a href="http://www.w3.org/TR/rdf-syntax-grammar#idAttr">idAttr</a> also
blocks
<a href="http://www.w3.org/TR/rdf-syntax-grammar#section-Reification">section-Reification</a>.
By default, rule
<a href="http://www.w3.org/TR/rdf-syntax-grammar#propertyAttr">propertyAttr</a>
is blocked. For the basic writer (RDF/XML) only
<a href="http://www.w3.org/TR/rdf-syntax-grammar#parseTypeLiteralPropertyElt">parseTypeLiteralPropertyElt</a>
has any effect, since none of the other rules are implemented by
that writer.</p>
</td>
<td><tt>Resource[]</tt> or <tt>String</tt></td>
<td></td>
</tr>
<tr>
<td><tt>prettyTypes</tt></td>
<td>
Only for the RDF/XML-ABBREV writer. This is a list of the types of
the principal objects in the model. The writer will tend to create
RDF/XML with resources of these types at the top level.
</td>
<td>
<tt>Resource[]</tt>
</td>
<td></td>
</tr>
</table>
<p>To set properties on the RDF/XML writer:</p>
<pre tabindex="0"><code> // Properties to be set.
Map&lt;String, Object&gt; properties = new HashMap&lt;&gt;() ;
properties.put(&#34;showXmlDeclaration&#34;, &#34;true&#34;);
RDFWriter.create()
.base(&#34;http://example.org/&#34;)
.format(RDFFormat.RDFXML_PLAIN)
.set(SysRIOT.sysRdfWriterProperties, properties)
.source(model)
.output(System.out);
</code></pre><p>See
<a href="https://github.com/apache/jena/blob/main/jena-examples/src/main/java/arq/examples/riot/ExRIOT_RDFXML_WriterProperties.java">ExRIOT_RDFXML_WriterProperties.java</a>.</p>
<p><b>Legacy example</b></p>
<p>As an example,</p>
<pre><code>RDFWriter w = m.getWriter(&quot;RDF/XML-ABBREV&quot;);
w.setProperty(&quot;attributeQuoteChar&quot;,&quot;'&quot;);
w.setProperty(&quot;showXMLDeclaration&quot;,&quot;true&quot;);
w.setProperty(&quot;tab&quot;,&quot;1&quot;);
w.setProperty(&quot;blockRules&quot;,
&quot;daml:collection,parseTypeLiteralPropertyElt,&quot;
+&quot;parseTypeResourcePropertyElt,parseTypeCollectionPropertyElt&quot;);
</code></pre>
<p>creates a writer that does not use rdf:parseType (preferring
rdf:datatype for rdf:XMLLiteral), indents only a little, and
produces the XMLDeclaration. Attributes are used, and are quoted
with <code>&quot;'&quot;</code>.</p>
<p>Note that property attributes are not used at all, by default.
However, the RDF/XML-ABBREV writer includes a rule to produce
property attributes when the value does not contain any spaces.
This rule is normally switched off. This rule can be turned on
selectively by using the blockRules property as detailed above.</p>
<h2 id="conformance">Conformance</h2>
<p>The RDF/XML I/O endeavours to conform with the
<a href="http://www.w3.org/TR/rdf-syntax-grammar/">RDF Syntax Recommendation</a>.</p>
<p>The parser must be set to strict mode. (Note that, the conformant
behaviour for <code>rdf:parseType=&quot;daml:collection&quot;</code> is to silently turn
<code>&quot;daml:collection&quot;</code> into <code>&quot;Literal&quot;</code>).</p>
<p>The RDF/XML writer is conformant, but does not exercise much of the
grammar.</p>
<p>The RDF/XML-ABBREV writer exercises all of the grammar and is
conformant except that it uses the <code>daml:collection</code> construct for
DAML ontologies. This non-conformant behaviour can be switched off
using the <code>blockRules</code> property.</p>
<h2 id="faster-rdfxml-io">Faster RDF/XML I/O</h2>
<p>To optimise the speed of writing RDF/XML it is suggested that all
URI processing is turned off. Also do not use RDF/XML-ABBREV. It is
unclear whether the longId attribute is faster or slower; the short
IDs have to be generated on the fly and a table maintained during
writing. The longer IDs are long, and hence take longer to write.
The following creates a faster writer:</p>
<pre><code>Model m;
RDFWriter fasterWriter = m.getWriter(&quot;RDF/XML&quot;);
fasterWriter.setProperty(&quot;allowBadURIs&quot;,&quot;true&quot;);
fasterWriter.setProperty(&quot;relativeURIs&quot;,&quot;&quot;);
fasterWriter.setProperty(&quot;tab&quot;,&quot;0&quot;);
</code></pre>
<p>When reading RDF/XML the check for reuse of rdf:ID has a memory
overhead, which can be significant for very large files. In this
case, this check can be suppressed by telling ARP to ignore this
error.</p>
<pre><code>Model m;
RDFReader bigFileReader = m.getReader(&quot;RDF/XML&quot;);
bigFileReader.setProperty(&quot;WARN_REDEFINITION_OF_ID&quot;,&quot;EM_IGNORE&quot;);
</code></pre>
</article>
<aside class="text-muted align-self-start mb-3 mb-xl-5 p-0 d-none d-xl-flex flex-column sticky-top">
<h2 class="h6 sticky-top m-0 p-2 bg-body-tertiary">On this page</h2>
<nav id="TableOfContents">
<ul>
<li><a href="#advanced-rdfxml-output">Advanced RDF/XML Output</a>
<ul>
<li><a href="#properties-to-control-rdfxml-output">Properties to Control RDF/XML Output</a></li>
</ul>
</li>
<li><a href="#conformance">Conformance</a></li>
<li><a href="#faster-rdfxml-io">Faster RDF/XML I/O</a></li>
</ul>
</nav>
</aside>
</main>
</div>
</div>
</div>
<footer class="bd-footer py-4 py-md-5 mt-4 mt-lg-5 bg-body-tertiary">
<div class="container" style="font-size:80%" >
<p>
Copyright &copy; 2011&ndash;2024 The Apache Software Foundation, Licensed under the
<a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.
</p>
<p>
Apache Jena, Jena, the Apache Jena project logo, Apache and the Apache feather logos are trademarks of
The Apache Software Foundation.
<br/>
<a href="https://privacy.apache.org/policies/privacy-policy-public.html"
>Apache Software Foundation Privacy Policy</a>.
</p>
</div>
</footer>
<script src="/js/popper.min.js.js" type="text/javascript"></script>
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
<script src="/js/improve.js" type="text/javascript"></script>
<script type="text/javascript">
(function() {
'use strict'
const links = document.querySelectorAll(`a[href="${window.location.pathname}"]`)
if (links !== undefined && links !== null) {
for (const link of links) {
link.classList.add('active')
let parentElement = link.parentElement
let count = 0
const levelsLimit = 4
while (['UL', 'LI'].includes(parentElement.tagName) && count <= levelsLimit) {
if (parentElement.tagName === 'LI') {
parentElement.querySelector('a:first-child').classList.add('active')
}
parentElement = parentElement.parentElement
count++
}
}
}
})()
</script>
</body>
</html>