Hi all, I'm trying to create an XMLLiteral and get the value from it.
(This is a reduced version of a larger problem, where I'm reading a
model that has some XMLLiterals, and I need to do some work with
them.) It appears that getValue() throws DatatypeFormat Exception
when this happens, though. Here's code that illustrates the problem:
import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.vocabulary.RDF;
public class ReadXMLLiterals {
public static void main( String[] args ) {
// Some XML content with newlines.
final String xmlContent = "<OMOBJ
xmlns='http://www.openmath.org/OpenMath'\n" +
" cdbase='http://www.openmath.org/cd'>\n" +
"</OMOBJ>";
// Create a model, and an XMLLiteral with the given content, and
// add a triple [] rdf:value xmlliteral so that something can be shown in the
// model, and to demonstrate that the literal can be created without error.
// Print the model.
System.out.println( "=============" );
final Model model = ModelFactory.createDefaultModel();
final Literal xmlLiteral = model.createTypedLiteral( xmlContent,
XMLLiteralType.theXMLLiteralType );
model.createResource().addProperty( RDF.value, xmlLiteral );
model.write( System.out, "N3" );
// Try to get the value of the literal and watch things fall apart.
// The error is that "Lexical form ... is not a legal instance of
// Datatype[http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral]
Bad rdf:XMLLiteral.
// How come there was no complaint when the literal was created?
And what's the
// problem with the lexical form?
System.out.println( "-------------" );
try {
xmlLiteral.getValue();
catch ( Exception e ) {
e.printStackTrace( System.out );
The output shows that the model can be created (and thus that the
XMLLiteral can be created) and serialized (showing the proper
datatype). However, when trying to use Literal#getValue to get a
value from the XMLLiteral, things explode:
=============
[] <http://www.w3.org/1999/02/22-rdf-syntax-ns#value>
"""<OMOBJ xmlns='http://www.openmath.org/OpenMath'
cdbase='http://www.openmath.org/cd'>
</OMOBJ>"""^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> .
com.hp.hpl.jena.datatypes.DatatypeFormatException: Lexical form
'<OMOBJ xmlns='http://www.openmath.org/OpenMath'
cdbase='http://www.openmath.org/cd'>
</OMOBJ>' is not a legal instance of
Datatype[http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral] Bad
rdf:XMLLiteral
at com.hp.hpl.jena.graph.impl.LiteralLabelImpl.getValue(LiteralLabelImpl.java:324)
at com.hp.hpl.jena.graph.Node_Literal.getLiteralValue(Node_Literal.java:39)
at com.hp.hpl.jena.rdf.model.impl.LiteralImpl.getValue(LiteralImpl.java:98)
at ReadXMLLiterals.main(ReadXMLLiterals.java:33)
Is something actually actually malformed with the the lexical form?
And if it is, should it have been caught when the literal was created?
(This is a reduced version of a larger problem, where I'm reading a
model that has some XMLLiterals, and I need to do some work with
them.) It appears that getValue() throws DatatypeFormat Exception
when this happens, though. Here's code that illustrates the problem:
import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.vocabulary.RDF;
public class ReadXMLLiterals {
public static void main( String[] args ) {
// Some XML content with newlines.
final String xmlContent = "<OMOBJ
xmlns='http://www.openmath.org/OpenMath'\n" +
" cdbase='http://www.openmath.org/cd'>\n" +
"</OMOBJ>";
// Create a model, and an XMLLiteral with the given content, and
// add a triple [] rdf:value xmlliteral so that something can be shown in the
// model, and to demonstrate that the literal can be created without error.
// Print the model.
System.out.println( "=============" );
final Model model = ModelFactory.createDefaultModel();
final Literal xmlLiteral = model.createTypedLiteral( xmlContent,
XMLLiteralType.theXMLLiteralType );
model.createResource().addProperty( RDF.value, xmlLiteral );
model.write( System.out, "N3" );
// Try to get the value of the literal and watch things fall apart.
// The error is that "Lexical form ... is not a legal instance of
// Datatype[http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral]
Bad rdf:XMLLiteral.
// How come there was no complaint when the literal was created?
And what's the
// problem with the lexical form?
System.out.println( "-------------" );
try {
xmlLiteral.getValue();
catch ( Exception e ) {
e.printStackTrace( System.out );
The output shows that the model can be created (and thus that the
XMLLiteral can be created) and serialized (showing the proper
datatype). However, when trying to use Literal#getValue to get a
value from the XMLLiteral, things explode:
=============
[] <http://www.w3.org/1999/02/22-rdf-syntax-ns#value>
"""<OMOBJ xmlns='http://www.openmath.org/OpenMath'
cdbase='http://www.openmath.org/cd'>
</OMOBJ>"""^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> .
com.hp.hpl.jena.datatypes.DatatypeFormatException: Lexical form
'<OMOBJ xmlns='http://www.openmath.org/OpenMath'
cdbase='http://www.openmath.org/cd'>
</OMOBJ>' is not a legal instance of
Datatype[http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral] Bad
rdf:XMLLiteral
at com.hp.hpl.jena.graph.impl.LiteralLabelImpl.getValue(LiteralLabelImpl.java:324)
at com.hp.hpl.jena.graph.Node_Literal.getLiteralValue(Node_Literal.java:39)
at com.hp.hpl.jena.rdf.model.impl.LiteralImpl.getValue(LiteralImpl.java:98)
at ReadXMLLiterals.main(ReadXMLLiterals.java:33)
Is something actually actually malformed with the the lexical form?
And if it is, should it have been caught when the literal was created?