Solved

Parse svg file in perl to create a simpler svg file

Posted on 2009-05-05
16
869 Views
Last Modified: 2012-05-06
I need help in parsing a svg file, shown below into a simpler format that can be used in a flex app. I want to parse all the visio metadata out and create a simpler version of svg

like

<svg><line x1="xxxx" y1="yyyy" x2="nnnn" y2="zzzz"/>
       <line x1="xx22" y1="yy22" x2="nn22" y2="zz22"/>
........................
</svg>
         

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">

<!-- Generated by Microsoft Visio 11.0, SVG Export, v1.0 30001423.svg Common Page -->

<svg xmlns="http://www.w3.org/2000/svg" xmlns:v="http://schemas.microsoft.com/visio/2003/SVGExtensions/" width="1.51867in"

		height="2.13542in" viewBox="0 0 109.344 153.75" xml:space="preserve" color-interpolation-filters="sRGB" class="st6">

	<title>20074902</title>

	<v:documentProperties v:langID="1033" v:viewMarkup="false"/>
 

	<style type="text/css">

	<![CDATA[

		.st1 {stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-width:1.5}

		.st2 {stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-width:0.8}

		.st3 {stroke:none;stroke-linecap:round;stroke-linejoin:round;stroke-width:0.75}

		.st4 {fill:#000000;font-family:Arial;font-size:0.499992em}

		.st5 {baseline-shift:-32.4949%;font-size:0.649897em}

		.st6 {fill:none;fill-rule:evenodd;font-size:12;overflow:visible;stroke-linecap:square;stroke-miterlimit:3}

	]]>

	</style>
 

	<g v:mID="4" v:index="1" v:groupContext="foregroundPage">

		<title>1 Column Wide, 1/3rd Page Depth</title>

		<v:pageProperties v:drawingScale="1" v:pageScale="1" v:drawingUnits="19" v:shadowOffsetX="9" v:shadowOffsetY="-9"/>

		<v:layer v:name="Connector" v:index="0"/>

		<v:layer v:name="Redline" v:index="1" color="#ff0000" v:print="false" v:snap="false" v:glue="false"/>

		<g id="shape5-1" v:mID="5" v:groupContext="shape" transform="translate(18.9296,-1.5)">

			<title>Sheet.5</title>

			<path d="M0 153.75 L71.77 153.75 L71.77 3 L0 3 L0 153.75 L0 153.75" class="st1"/>
 

		</g>

		<g id="shape6-4" v:mID="6" v:groupContext="shape" transform="translate(0.987871,-139.402)">

			<title>Sheet.6</title>

			<path d="M17.94 153.75 L0 153.75" class="st2"/>

		</g>

		<g id="shape7-7" v:mID="7" v:groupContext="shape" transform="translate(0.987871,-121.37)">

			<title>Sheet.7</title>

			<path d="M17.94 153.75 L0 153.75" class="st2"/>
 

		</g>

		<g id="shape8-10" v:mID="8" v:groupContext="shape" transform="translate(0.987871,-103.338)">

			<title>Sheet.8</title>

			<path d="M17.94 153.75 L0 153.75" class="st2"/>

		</g>

		<g id="shape9-13" v:mID="9" v:groupContext="shape" transform="translate(0.987871,-85.4007)">

			<title>Sheet.9</title>

			<path d="M17.94 153.75 L0 153.75" class="st2"/>
 

		</g>

		<g id="shape10-16" v:mID="10" v:groupContext="shape" transform="translate(0.987871,-67.3689)">

			<title>Sheet.10</title>

			<path d="M17.94 153.75 L0 153.75" class="st2"/>

		</g>

		<g id="shape11-19" v:mID="11" v:groupContext="shape" transform="translate(90.6964,-139.402)">

			<title>Sheet.11</title>

			<path d="M0 153.75 L17.85 153.75" class="st2"/>
 

		</g>

		<g id="shape12-22" v:mID="12" v:groupContext="shape" transform="translate(90.6964,-121.37)">

			<title>Sheet.12</title>

			<path d="M0 153.75 L17.85 153.75" class="st2"/>

		</g>

		<g id="shape13-25" v:mID="13" v:groupContext="shape" transform="translate(90.6964,-103.338)">

			<title>Sheet.13</title>

			<path d="M0 153.75 L17.85 153.75" class="st2"/>
 

		</g>

		<g id="shape14-28" v:mID="14" v:groupContext="shape" transform="translate(90.6964,-85.4007)">

			<title>Sheet.14</title>

			<path d="M0 153.75 L17.85 153.75" class="st2"/>

		</g>

		<g id="shape15-31" v:mID="15" v:groupContext="shape" transform="translate(90.6964,-67.3689)">

			<title>Sheet.15</title>

			<path d="M0 153.75 L17.85 153.75" class="st2"/>
 

		</g>

</g>

Open in new window

0
Comment
Question by:gagangill
  • 6
  • 6
  • 4
16 Comments
 
LVL 11

Expert Comment

by:kblack05
ID: 24315469
There is a module designed for this called SVG::Parser.

Here is the module page complete with example scripting:

http://search.cpan.org/~peterw/SVG-Parser-1.03/lib/SVG/Parser.pm
0
 

Author Comment

by:gagangill
ID: 24316463
Thanks

Do I need to install this module on the server, where can I get the module
0
 
LVL 11

Expert Comment

by:kblack05
ID: 24316807
There are some dependent modules that also would need to be installed. Here is the process as it relates to a Unix/Linux type system, you'll need to be "super user" or logged in as ROOT user.

Here is how you can test using "perl -e" if the module is installed. This was on a server that does NOT have it installed so you can see what it looks like:

[kblack@www01 ~]$ perl -e 'use SVG::Parser'
Can't locate SVG/Parser.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at -e line 1.
BEGIN failed--compilation aborted at -e line 1.


Then I installed it with

(As root user)
(If the installer asks you to follow dependencies, or install them, say YES)

perl -MCPAN -e 'install XML::Parser';
perl -MCPAN -e 'install XML::SAX';
perl -MCPAN -e 'install XML::SAX::Expat';
perl -MCPAN -e 'install SVG::Parser::SAX';
perl -MCPAN -e 'install SVG::Parser';

If all goes well you'll be able to test with clean output:

[kblack@www01 ~]$ perl -e 'use SVG::Parser'

[kblack@www01 ~]$
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24317507
The CPAN module will handle installing all of the dependencies, as long as the module is configured correctly.  So you should only need to install the SVG::Parser, and everything else that is needed will be installed.
0
 

Author Comment

by:gagangill
ID: 24319783
I installed the modules and then ran the script with an argument, but it does not strip out the visio elements, it just takes the file and spits it out


Here is the perl script

#!/usr/bin/perl -w

use XML::Simple;
use LWP::Simple;
use strict;
  use SVG::Parser;

  die "Usage: $0 <file>\n" unless @ARGV;

  my $xml;
  {
      local $/=undef;
      $xml=<>;
  }

  my $parser=new SVG::Parser(-debug => 1);
  my $svg=$parser->parse($xml);
  print $svg->xmlify;
0
 
LVL 11

Expert Comment

by:kblack05
ID: 24325759
Can you help my by providing some specific example data? I'm sure we can help you get what you want from the programming, if you can give us a little more exact information about what you are trying to do.

Thanks
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24329614
In your original post, what is the logic used to get from the visio output to your desired output?
0
 

Author Comment

by:gagangill
ID: 24336631
My goal is to get a visio file that is saved in svg into flex app and I render it there. Somebody suggested not in this fourm that the best way would be to clean the visio generated svg file in perl first to create a clean svg file and then bring that in my Flex app and render it there.

Maybe this is not a great idea. So now I am thinking that we should use an svg parser in Flex that could read the visio generated svg and ignore all the visio elements and render it in Flex. Will this work, if yes can you please guide me
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 11

Expert Comment

by:kblack05
ID: 24336822
Perl has some interesting capabilities here. For example, lets say you want to iterate over the following line and not include it in the output:

<v:layer v:name="Connector" v:index="0"/>

for (@lines) {
next if ($_ =~ /v\:/smg);
print;
}
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24338899
Does your Flex app not read in the svg file as generated by visio?  
0
 

Author Comment

by:gagangill
ID: 24339224
After I read the svg file I need to put the line/rectangle/circle values in an array collection and then draw the graphics
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24339325
So how would you like a perl program to determine how to read and rewrite the svg file?  Is there some way to determine what should go in an array collection?
0
 

Author Comment

by:gagangill
ID: 24349900
I want to get the following from the original visio

style array
st1 = {stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-width:1.5}
st2 = {stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-width:0.8}
...
st6 = {fill:none;fill-rule:evenodd;font-size:12;overflow:visible;stroke-linecap:square;stroke-miterlimit:3}

then in the line array
transform = (18.9296,-1.5)
d="M0 153.75 L71.77 153.75 L71.77 3 L0 3 L0 153.75 L0 153.75"
........
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24426724
So, for output, you'd want:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:v="http://schemas.microsoft.com/visio/2003/SVGExtensions/"

  width="1.51867in" height="2.13542in" viewBox="0 0 109.344 153.75" xml:space="preserve"

  color-interpolation-filters="sRGB" class="st6">

    <style type="text/css">

        <![CDATA[

            .st1 {stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-width:1.5}

            .st2 {stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-width:0.8}

            .st3 {stroke:none;stroke-linecap:round;stroke-linejoin:round;stroke-width:0.75}

            .st4 {fill:#000000;font-family:Arial;font-size:0.499992em}

            .st5 {baseline-shift:-32.4949%;font-size:0.649897em}

            .st6 {fill:none;fill-rule:evenodd;font-size:12;overflow:visible;stroke-linecap:square;stroke-miterlimit:3}

        ]]>

    </style>

    <g v:mID="4" v:index="1" v:groupContext="foregroundPage">

        <g id="shape5-1" v:mID="5" v:groupContext="shape" transform="translate(18.9296,-1.5)">

            <path d="M0 153.75 L71.77 153.75 L71.77 3 L0 3 L0 153.75 L0 153.75" class="st1"/>

        </g>

    </g>

</svg>

Open in new window

0
 

Author Comment

by:gagangill
ID: 24742320
Hi Adam,

I need to parse this further in flex, so that I can get g id, the path d after calculating the transform, all these need to be stored in an array and displayed in a data grid and then drawn in a can canvas.

Can you please help
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24797226
I'm not familiar with flex, so I can't help with that part.  If you have some rules that determine what should be kept/removed, I can help put that in to code.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl to convert excel to csv 3 150
iSeries PERL Scripts 7 129
Any syntax error for this clone.plscript 6 126
Perl string filter 5 64
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now