Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: which is the best way to convert xml to GraphML using perl.

by Khen1950fx (Canon)
on Nov 17, 2011 at 12:08 UTC ( #938605=note: print w/ replies, xml ) Need Help??


in reply to which is the best way to convert xml to GraphML using perl.

The Graph::Easy distribution will do the job. To get a GraphML text, use Graph::Easy::As_graphml. Here's the documentation's example. I saved the output to file.

#!/usr/bin/perl -slw use strict; use Graph::Easy; my $graph = Graph::Easy->new(); $graph->add_edge( 'Bonn', 'Berlin' ); open STDOUT, '>', 'graph.graphml'; binmode STDOUT, ':encoding(UTF-8)'; print $graph->as_graphml(); close STDOUT;
Outputs:
<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <!-- Created by Graph::Easy v0.70 at Thu Nov 17 03:13:06 2011 --> <graph id="G" edgedefault="directed"> <node id="Berlin"> </node> <node id="Bonn"> </node> <edge source="Bonn" target="Berlin"> </edge> </graph> </graphml>
Use Graph::Easy::Parser to create a Graph::Easy object that you can convert to a graph. I used the as_ascii method.
#!/usr/bin/perl -slw use strict; use Graph::Easy; use Graph::Easy::Parser; my $parser = Graph::Easy::Parser->new(); print $parser->from_text('[Bonn]->[Berlin]')->as_ascii();
Give it try. Good luck.


Comment on Re: which is the best way to convert xml to GraphML using perl.
Select or Download Code
Re^2: which is the best way to convert xml to GraphML using perl.
by veerubiji (Sexton) on Nov 21, 2011 at 12:23 UTC

    thnks for your reply, I will try like this.

Re^2: which is the best way to convert xml to GraphML using perl.
by veerubiji (Sexton) on Nov 22, 2011 at 13:40 UTC

    I am unable to convert this xml file to GraphML format. can you give example how to convert xml to GraphML format using Graph::easy.

    example.xml <?xml version="1.0" encoding="UTF-8" ?> <specification xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <orderinfo> <servicename>ssc</servicename> <Customer>dvr</Customer> <Suppliers> <Supplier Id="L7a" /> </Suppliers> </orderinfo> </specification>

    I need to disply like servicename , customer, supplier as three nodes and edges from supplier to service name and service name to customer.I have large xml file like with same type of data. can you help or provide some examples.

      It's confusing at first, but once you actually try to do something with it, it works. Here's a simple script to get you started. I just added a couple of edges, but I think that you'll be able to add some nodes and attributes after awhile.
      #!/usr/bin/perl use strict; use warnings; use Graph::Easy qw/as_graphml as_graphml_file/; my $graph = Graph::Easy->new(); my $graphml_file = $graph->as_graphml_file(); $graphml_file =~ s/\n.*<!--.*-->\n//; my $graphml = $graph->as_graphml(); $graph->add_edge('orderinfo', 'servicename'); binmode STDOUT, ':encoding(UTF-8)'; print $graphml = $graph->as_graphml();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938605]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-09-23 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (235 votes), past polls