In Java, using Jena, I do things like this using a ByteArrayInputStream to create an input stream from the RDF text that I can then pass to the parsing functions (which typically accept both URIs and InputStreams. The docs for Graph.parse say that
parse(source=None, publicID=None, format=None, location=None, file=None, data=None, **args)
Parameters:
- source: An InputSource, file-like object, or string. In the case of a string the string is the location of the source.
Update: lawlesst provided an answer that points out that the data parameter to Graph.parse can be a string containing RDF content. The documentation mentions that now, but I don't think it did when I originally answered. lawlesst's answer should be preferred, in my opinion.
So to apply the same idiom, you just need to create an InputSource or “file-like object” from your string. I'm not much of a Pythonista, so I originally thought you'd want to take the InputSource route, but it appears that that's a SAX parser thing. What you probably want is Python's StringIO:
7.5. StringIO — Read and write strings as files
This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files). See the description of file objects for operations (section File Objects). (For standard strings, see str and unicode.)
class StringIO.StringIO([buffer])
When a StringIO object is created, it can be initialized to an existing string by passing the string to the constructor. If no string is given, the StringIO will start empty. In both cases, the initial file position starts at zero.
So it seems like if you create a StringIO with your given text, then you can pass it as the first argument to Graph.parse. As I said, I'm not really a Python user, but I think it would look something like this:
block = '''<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
…
</rdf:RDF>'''
Graph g;
g.parse( StringIO.StringIO(block), format='xml')
Update: Based on lawlesst's answer this can be simplified without the StringIO as just:
g.parse( data=block, format='xml' )