A high-performance RDF/XML decoder and encoder for rdf_core implementing the W3C standard
pub.dev Get StartedFull implementation of the W3C RDF/XML specification with support for all standard features.
Optimized for both speed and memory efficiency.
Flexible configuration for strict or lenient decoding, formatting options, and more.
Built on SOLID principles with dependency injection for easy testing and customization.
Easily extend or customize the behavior to meet specific application requirements.
Comprehensive test suite covering both standard cases and real-world RDF/XML examples.
Add rdf_xml
to your Dart project:
dart pub add rdf_xml
Or add it to your pubspec.yaml
:
# pubspec.yaml dependencies: rdf_xml: ^0.2.4
import 'package:rdf_xml/rdf_xml.dart'; void main() { final xmlContent = ''' <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> <rdf:Description rdf:about="http://example.org/resource"> <dc:title>Example Resource</dc:title> <dc:creator>Example Author</dc:creator> </rdf:Description> </rdf:RDF> '''; // Use the global rdfxml codec final rdfGraph = rdfxml.decode(xmlContent); // Print the decoded triples for (final triple in rdfGraph.triples) { print(triple); } }
import 'package:rdf_core/rdf_core.dart'; import 'package:rdf_xml/rdf_xml.dart'; void main() { // Create a graph with some triples final graph = RdfGraph.fromTriples([ Triple( IriTerm('http://example.org/resource'), IriTerm('http://purl.org/dc/elements/1.1/title'), LiteralTerm.string('Example Resource'), ), Triple( IriTerm('http://example.org/resource'), IriTerm('http://purl.org/dc/elements/1.1/creator'), LiteralTerm.string('Example Author'), ), ]); // Use the global rdfxml codec final rdfXml = rdfxml.encode(graph); print(rdfXml); }
import 'package:rdf_core/rdf_core.dart'; import 'package:rdf_xml/rdf_xml.dart'; void main() { // Define some RDF/XML content final xmlContent = ''' <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"> <rdf:Description rdf:about="http://example.org/resource"> <dc:title>Example Resource</dc:title> <dc:creator>Example Author</dc:creator> </rdf:Description> </rdf:RDF> '''; // Register the codec with RdfCore final rdfCore = RdfCore.withStandardCodecs(additionalCodecs: [RdfXmlCodec()]); // Decode RDF/XML content final rdfGraph = rdfCore.decode(xmlContent); // Print the decoded triples for (final triple in rdfGraph.triples) { print(triple); } // Create a new graph with some triples final newGraph = RdfGraph.fromTriples([ Triple( IriTerm('http://example.org/resource'), IriTerm('http://purl.org/dc/elements/1.1/title'), LiteralTerm.string('Example Resource'), ), Triple( IriTerm('http://example.org/resource'), IriTerm('http://purl.org/dc/elements/1.1/creator'), LiteralTerm.string('Example Author'), ), ]); // Encode using RdfCore with specified content type final rdfXml = rdfCore.encode(newGraph, contentType: "application/rdf+xml"); print(rdfXml); }
import 'dart:io'; import 'package:rdf_core/rdf_core.dart'; import 'package:rdf_xml/rdf_xml.dart'; Future<void> parseFromFile(String filePath) async { final file = File(filePath); final xmlContent = await file.readAsString(); // Decode with base URI set to the file location final rdfGraph = rdfxml.decode( xmlContent, documentUrl: 'file://${file.absolute.path}', ); print('Parsed ${rdfGraph.size} triples from $filePath'); }
The decoder can be configured for different use cases:
// Create a codec with strict validation final strictCodec = RdfXmlCodec.strict(); // Create a codec that handles non-standard RDF/XML final lenientCodec = RdfXmlCodec.lenient(); // Custom configuration final customCodec = RdfXmlCodec( decoderOptions: RdfXmlDecoderOptions( strictMode: false, normalizeWhitespace: true, validateOutput: true, maxNestingDepth: 50, ), );
The encoder can also be configured for different output requirements:
// Human-readable output final readableCodec = RdfXmlCodec.readable(); // Compact output for storage final compactCodec = RdfXmlCodec.compact(); // Custom configuration final customCodec = RdfXmlCodec( encoderOptions: RdfXmlEncoderOptions( prettyPrint: true, indentSpaces: 4, useNamespaces: true, useTypedNodes: true, ), );
This library implements all features of the RDF/XML syntax as defined by the W3C:
Explore the complete API documentation for detailed information about all classes and methods.
View API Documentation