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

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.
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, http://www.liquid-technologies.com/xml-editor.aspx.
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 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

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;
    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.

      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="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: perlquestion [id://938548]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-12-25 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (161 votes), past polls