Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <pre><code>// Construct DOM elements $xml = new DomDocument('1.0', 'UTF-8'); $xml-&gt;formatOutput = true; $gexf = $xml-&gt;createElementNS(null, 'gexf'); $gexf = $xml-&gt;appendChild($gexf); // Assign namespaces for GexF with VIZ :) $gexf-&gt;setAttribute('xmlns:viz', 'http://www.gexf.net/1.1draft/viz'); // Skip if you dont need viz! $gexf-&gt;setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $gexf-&gt;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-&gt;appendChild($xml-&gt;createElement('meta')); $meta-&gt;setAttribute('lastmodifieddate', date('Y-m-d')); $meta-&gt;appendChild($xml-&gt;createElement('creator', 'PHP GEXF Generator v0.1')); $meta-&gt;appendChild($xml-&gt;createElement('description', 'by me etc')); // Add Graph data! $graph = $gexf-&gt;appendChild($xml-&gt;createElement('graph')); $nodes = $graph-&gt;appendChild($xml-&gt;createElement('nodes')); $edges = $graph-&gt;appendChild($xml-&gt;createElement('edges')); // Add Node! $node = $xml-&gt;createElement('node'); $node-&gt;setAttribute('id', '1'); $node-&gt;setAttribute('label', 'Hello world!'); // Set color for node $color = $xml-&gt;createElement('viz:color'); $color-&gt;setAttribute('r', '1'); $color-&gt;setAttribute('g', '1'); $color-&gt;setAttribute('b', '1'); $node-&gt;appendChild($color); // Set position for node $position = $xml-&gt;createElement('viz:position'); $position-&gt;setAttribute('x', '1'); $position-&gt;setAttribute('y', '1'); $position-&gt;setAttribute('z', '1'); $node-&gt;appendChild($position); // Set size for node $size = $xml-&gt;createElement('viz:size'); $size-&gt;setAttribute('value', '1'); $node-&gt;appendChild($size); // Set shape for node $shape = $xml-&gt;createElement('viz:shape'); $shape-&gt;setAttribute('value', 'disc'); $node-&gt;appendChild($shape); // Add Edge (assuming there is a node with id 2 as well!) $edge = $xml-&gt;createElement('edge'); $edge-&gt;setAttribute('source', '1'); $edge-&gt;setAttribute('target', '2'); // Commit node &amp; edge changes to nodes! $edges-&gt;appendChild($edge); $nodes-&gt;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-&gt;saveXML(); </code></pre> <p>Eat your heart out! Feel free to email me your project results, I am curious.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload