Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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
[GotToBTru]: late to the conversation .. but thanks, Corion, for the reminder about Perl::Tidy
[Corion]: GotToBTru: Heh ;) I'm not really endorsing it, as I found that any gains made through it are easily squandered by the hours spent on configuring it. At least in my case :)
[GotToBTru]: Like most Swiss Army Knives, you can cut yourself trying to find the blade you actually want
[GotToBTru]: i am satisfied with the default settings turning the seemingly random formatting into something reliable
[Corion]: GotToBTru: Yeah, I started a quick(ly implemented but slowly running) implementation of a program that would guess the "best" configuration (that is, least amount of changes) from my code, but then found that it stops parsing the source as soon as it ..
[GotToBTru]: sadly, it can't do anything about this

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-02-27 14:30 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (387 votes). Check out past polls.