0

I need to automatically create GEXF (http://gexf.net) XML files from an array of nodes in PHP.

I've Googled the topic but was unable to find anything useful.

How would I do this?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
thedp
  • 8,350
  • 16
  • 53
  • 95
  • I don't know any lib for GEXF specifically, but you can always use any of the generic XML extensions, e.g. http://stackoverflow.com/questions/188414/best-xml-parser-for-php/3616044#3616044 – Gordon Oct 01 '12 at 17:45
  • I really want to avoid writing my own. It's not that easy to construct such an XML from a complex network of nodes. – thedp Oct 01 '12 at 18:10

1 Answers1

3

Here is my take on this. After a number of hours I got it all right. This example has support for the viz: namespace too, so you can use more detailed node elements and placements.

// Construct DOM elements
$xml = new DomDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$gexf = $xml->createElementNS(null, 'gexf');
$gexf = $xml->appendChild($gexf);

// Assign namespaces for GexF with VIZ :)
$gexf->setAttribute('xmlns:viz', 'http://www.gexf.net/1.1draft/viz'); // Skip if you dont need viz!
$gexf->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$gexf->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation', 'http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd');

// Add Meta data
$meta = $gexf->appendChild($xml->createElement('meta'));
$meta->setAttribute('lastmodifieddate', date('Y-m-d'));
$meta->appendChild($xml->createElement('creator', 'PHP GEXF Generator v0.1'));
$meta->appendChild($xml->createElement('description', 'by me etc'));

// Add Graph data!
$graph = $gexf->appendChild($xml->createElement('graph'));
$nodes = $graph->appendChild($xml->createElement('nodes'));
$edges = $graph->appendChild($xml->createElement('edges'));

    // Add Node!
    $node = $xml->createElement('node');
    $node->setAttribute('id', '1');
    $node->setAttribute('label', 'Hello world!');

        // Set color for node
        $color = $xml->createElement('viz:color');
        $color->setAttribute('r', '1');
        $color->setAttribute('g', '1');
        $color->setAttribute('b', '1');
        $node->appendChild($color);

        // Set position for node
        $position = $xml->createElement('viz:position');
        $position->setAttribute('x', '1');
        $position->setAttribute('y', '1');
        $position->setAttribute('z', '1');
        $node->appendChild($position);

        // Set size for node
        $size = $xml->createElement('viz:size');
        $size->setAttribute('value', '1');
        $node->appendChild($size);

        // Set shape for node
        $shape = $xml->createElement('viz:shape');
        $shape->setAttribute('value', 'disc');
        $node->appendChild($shape);

    // Add Edge (assuming there is a node with id 2 as well!)
    $edge = $xml->createElement('edge');
    $edge->setAttribute('source', '1');
    $edge->setAttribute('target', '2');     

// Commit node & edge changes to nodes!
$edges->appendChild($edge);
$nodes->appendChild($node);

    // Serve file as XML (prompt for download, remove if unnecessary)
    header('Content-type: "text/xml"; charset="utf8"');
    header('Content-disposition: attachment; filename="internet.gexf"');

// Show results!
echo $xml->saveXML();

Eat your heart out! Feel free to email me your project results, I am curious.

Gerben
  • 111
  • 1
  • 8