Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

by veerubiji (Sexton)
on Nov 17, 2011 at 09:02 UTC ( #938548=perlquestion: print w/replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have xml file and I need to represent in graph model so I chosen yEd graph editor to represent in graph model. But yEd graph editor is not support to xml, so I need to convert XML into GraphML file then I can use that GraphML format in yEd graph editor.Can any one suggest me how to convert xml to GraphML format using perl, probably with some examples.Because I am very new beginner.

  • Comment on which is the best way to convert xml to GraphML using perl.

Replies are listed 'Best First'.
Re: which is the best way to convert xml to GraphML using perl.
by Khen1950fx (Canon) on Nov 17, 2011 at 12:08 UTC
    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;
    <?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="" xmlns:xsi="" xsi:schemaLocation=""> <!-- 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.

      thnks for your reply, I will try like this.

      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=""> <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();
Re: which is the best way to convert xml to GraphML using perl.
by marto (Bishop) on Nov 17, 2011 at 09:30 UTC

    "But yEd graph editor is not support to xml, so I need to convert XML into GraphML file then I can use that GraphML format in yEd graph editor."

    Firstly yEd states:

    "yEd can import data in various formats to generate diagrams out of it. Import formats include the Microsoft Excel .xls format for spreadsheet data, the Gedcom format for genealogical data, and also arbitrary XML-based file formats, which are transformed by means of XSLT stylesheets. Predefined XSLT stylesheets provided by yEd can process the Apache Ant build script format used to define dependency information in software build processes and the OWL file format for the description of ontologies. Other XML-based data is processed in a generic way."

    I suggest you confirm this, and test to see if your XML can be used as an input. Had you super searched you'd have found nodes where people claim to have used yEd with XML, I dare say a google search would also be helpful.

    Secondly, since you came to the conclusing that yEd didn't support xml, why did you choose it as the product to use in the first place?

      sorry, I posted little bit confusing, Actually my problem is I have the input format as xml and I need to represent in model graph so I chosen yEd graph editor, It import xml data and displaying tree structured graph, I need to display in some other way so I need to convert it into GraphML. GraphML is the native format for yEd so I am trying like that.Can you suggest which perl module is good doing this.

        So your strategy, is to ignore all questions put to you, and simply repeat your original question, in a slightly more verbose way?

        Sure, the answer is, any one of these (and more): XML::Twig, XML::LibXML, XML::Rules, xsh, Graph::Easy::As_graphml

        There are tons of docs, tons of examples included, lots of examples at perlmonks...

        I suggest you actually use the sites super search facility, or try a google search. You've posted several questions recently relating to XML. IMHO you should make some effort to search for solutions.

Re: which is the best way to convert xml to GraphML using perl.
by Anonymous Monk on Nov 17, 2011 at 09:16 UTC

    The best way, is to not do it at all

    Find a tool that does it, it must exist already

      i totally agree find a tool that does graphical representation like an xml editor, i think liquid xml editor can do it,
Re: which is the best way to convert xml to GraphML using perl.
by mrguy123 (Hermit) on Nov 17, 2011 at 11:36 UTC
    Can you give us a small example of how your XML looks, and how you want the GraphML format to look?
    It will give us a better understanding on what exactly you want to do

      can you have a look at my latest reply, in that I posted my sample xml code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://938548]
Approved by marto
[karlgoethebier]: Cojones! We need cojones!
[karlgoethebier]: Ouch! Permissions! We need permissions!
[BarApp]: I can not use modules. I gain temporary access and still can not use modules.
[Cosmic37]: ta erix - this szabo geezer is pretty cool methinks and he writes about undef but I cannot see instructions for redefining the record separator after having undefined it
[Corion]: $/ = "wahtever";
[Corion]: (it's a magic variable)
[karlgoethebier]: BarApp: whoami
[Cosmic37]: ok fankyou - I was wondering about that but thought there might be a redefine command or something; peachy
[Lotus1]: Cosmic37 if you undef $/ in a local context to a block it won't affect the global version after the block finishes
[Lotus1]: or you can just do local $/ in a block

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-06-29 16:45 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (672 votes). Check out past polls.