RDF/XML for Dart

A high-performance RDF/XML decoder and encoder for rdf_core implementing the W3C standard

pub.dev Get Started
rdf_xml logo

Key Features

W3C Compliant

Full implementation of the W3C RDF/XML specification with support for all standard features.

High Performance

Optimized for both speed and memory efficiency.

Highly Configurable

Flexible configuration for strict or lenient decoding, formatting options, and more.

Clean Architecture

Built on SOLID principles with dependency injection for easy testing and customization.

Extensible Design

Easily extend or customize the behavior to meet specific application requirements.

Well Tested

Comprehensive test suite covering both standard cases and real-world RDF/XML examples.

Installation

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

Usage

Decoding RDF/XML

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);
  }
}

Encoding to RDF/XML

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);
}

Integration with RdfCore

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);
}
print(rdfXml); }

Decoding from a File

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');
}

Configuration

Decoder Options

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,
  ),
);

Encoder Options

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,
  ),
);

Supported RDF/XML Features

This library implements all features of the RDF/XML syntax as defined by the W3C:

  • Resource descriptions (rdf:Description)
  • Typed node elements
  • Property elements
  • Container elements (rdf:Bag, rdf:Seq, rdf:Alt)
  • Collection elements (rdf:List)
  • rdf:parseType (Resource, Literal, Collection)
  • XML Base support
  • XML language tags and datatyped literals
  • Blank nodes (anonymous and labeled)
  • RDF reification

API Documentation

Explore the complete API documentation for detailed information about all classes and methods.

View API Documentation