[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 938
  • Last Modified:

Parse svg file in perl to create a simpler svg file

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
gagangill
Asked:
gagangill
  • 6
  • 6
  • 4
1 Solution
 
kblack05Commented:
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
 
gagangillAuthor Commented:
Thanks

Do I need to install this module on the server, where can I get the module
0
 
kblack05Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Adam314Commented:
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
 
gagangillAuthor Commented:
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
 
kblack05Commented:
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
 
Adam314Commented:
In your original post, what is the logic used to get from the visio output to your desired output?
0
 
gagangillAuthor Commented:
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
 
kblack05Commented:
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
 
Adam314Commented:
Does your Flex app not read in the svg file as generated by visio?  
0
 
gagangillAuthor Commented:
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
 
Adam314Commented:
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
 
gagangillAuthor Commented:
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
 
Adam314Commented:
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
 
gagangillAuthor Commented:
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
 
Adam314Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now