Parsing RDF files with PHP and SimpleXML

A question from comp.lang.php

Here is an XML file I’d like to parse with php.
It’s a mozilla install.rdf file.

[RDF skipped]

How would you do that ?

If you want to parse RDF with SimpleXML, you should keep in mind that in RDF, XML entities’ names contain colons, which creates problems when you try to parse an XML document into an object, since columns are illegal in PHP variable names. You can, however, replace colons with underscores:

$RDF = file_get_contents('install.rdf');
$RDF = str_replace('<RDF:', '<RDF_', $RDF);
$RDF = str_replace('<em:', '<em_', $RDF);
$XML = simplexml_load_string($RDF);
foreach ($XML->RDF_Description as $num => $description) {
  $attributes = $description->attributes();
  if (isset($attributes['em_id'])) {
    echo "ID = {$attributes['em_id']};",
         " version = {$attributes['em_version']}";

This should help…

Leave a Reply

Your email address will not be published. Required fields are marked *