Page 1 of 1

write XML with PHP based on a schema

Posted: Thu Dec 02, 2010 7:41 am
by phersonw
Hi ~there!!

I need to write XML with PHP based on a particular schema. Actually it should based on GraphML schema.

My xml file is my friends list including "name","uid","pic",etc. How to write and create a new xml??

My xml :
<?xml version="1.0"?>
<friends>
<friend>
<uid>9876543210</uid>
<name>friend's name</name>
<sex>male</sex>
<pic>http: //xxxxxx.jpg</pic>
</friend>

I need to output like this:
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns=" http: //graphml.graphdrawing.org/xmlns"xmlns:xsi="http: //www.w3.org/2001/XMLSchema-instance"xsi: ... tion="http: //graphml.graphdrawing.org/xmlns http //graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">

<key id="sex" for="node" attr.name="sex" attr.type="string"/>
<key id="pic" for="node" attr.name="pic" attr.type="string"/>
<graph id="G" edgedefault="undirected">


<node id="friend's name">
<data key="sex">male</data>
<data key="pic">http: //xxxxxxx.jpg</data></node>
......



<edge source="friendA" target="friendB"></edge>
<edge source="friendA" target="friendC"></edge>
<edge source="friendD" target="friendA"></edge>
......


</graph>
</graphml>
PS: friendB and friendC are mutual friend of me and friend A, friendA is mutual friend of me and friend D.

Thanks !

Pherson

Re: write XML with PHP based on a schema

Posted: Thu Dec 02, 2010 3:30 pm
by requinix
The source XML has no mention of friends so I don't know where you'll get those <edge>s from.

If you take the nodes from any <friend> element, discard the <uid> and <name>, then do some simple numeric-or-not tests on the values from the test, you can create the <key> nodes fairly easily. Then for each <node> and <data>, you just run a loop over all the <friend> nodes and extract the node types that you got earlier (when you created the <key>s).

Re: write XML with PHP based on a schema

Posted: Thu Dec 02, 2010 10:11 pm
by phersonw
Hi, tasairis
Thanks for your help!

Actually the xml file is created by myself so I can modify it.
But I am not quite clear about what you said.

Could you explain more? It should be admirable that providing a example(programming) to me?

Thanks!

pherson

Re: write XML with PHP based on a schema

Posted: Thu Dec 02, 2010 11:22 pm
by requinix
phersonw wrote:It should be admirable that providing a example(programming) to me?
... Why yes: someone has, in fact, really been far even as decided to use even go want to do look more like.

Use something like SimpleXML to parse the XML feed. At the same time you can create the XML output.
Or you could try your hand at XSLT.
Or you could modify whatever produces the XML to produce this GraphML thing instead/too.