Parsing SVG with an external program in Java

Discuss SVG code, accessible via the XML Editor.
scruffydog
Posts: 13
Joined: Thu Nov 15, 2012 12:14 pm

Parsing SVG with an external program in Java

Postby scruffydog » Fri Jul 17, 2015 1:38 pm

Hello,

I want to write a Java program that will load Inkscape SVG and parse it. Specifically, I want to be able extract a particular path out of the SVG by its id attribute and do some additional processing with it. This processing may include finding its length, plotting a point a distance along it, and displaying it on the screen. The Batik library is one possible answer, but I am having a difficult time learning it. Is there a simpler library? In either case, could some sample code be provided?

Thanks,
Craig

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: Parsing SVG with an external program in Java

Postby hulf2012 » Sat Jul 18, 2015 1:02 am

Hello

Just to point to SNAP, for example:
http://www.fabacademy.org/archives/2014 ... ngSnapSVG/

As far as I know, there was a specific subset of javacript or (ecmascript?), for dealing with SVG. There were specific functions to get the length, or the bounding box, or the style of SVG objects. It wasn't neccesary to include external libraries.

Now... how to find it in google ??

Greetings
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Parsing SVG with an external program in Java

Postby brynn » Sat Jul 18, 2015 5:59 pm

I don't know enough about JS libraries to be able to compare them. I actually just recently learned what a JS library is. I've heard about Snap.svg, and D3.js Afaiu, D3.js is quite powerful, but not very focussed on SVG; while Snap.svg is smaller, but is more focussed on SVG. Note that's my very, very newbie impression (newbie to JS, not to Inkscape), and I might not have explained what I mean in a technically correct way.

I'm in the process (STILL!) of writing a page for the Inkscape website about animation and Inkscape. Although animation may not be exactly what you're after (I'm not sure what "parsing" is), you still may find some help with some links at the bottom of this draft of the page. Unfortunately, it's exactly the section you need, that hasn't been written yet. But you might still find some help with the links there. Note that some of them are not really links, just URLs. But afaik, they are correct.

Actually, I'm not sure if you can see the page. Let's try it. Please note this is a draft, and may change significantly before it's ready to go live on the website. Can you see this: https://inkscape.org/en/animation/ ? If you can't see it, I'll copy some links over here for you :D

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Parsing SVG with an external program in Java

Postby ~suv » Sat Jul 18, 2015 6:12 pm

Just a hint (@hulf2012, @brynn): JavaJavaScript.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Parsing SVG with an external program in Java

Postby brynn » Sat Jul 18, 2015 6:39 pm

Right ~suv! I know that, and I noticed that in the message, and I thought about it. But in all the 8 years I've been learning Inkscape, I've never heard of anyone using Java with Inkscape or with SVG. And I've never seen a message in this forum about using Java with Inkscape or SVG. So I thought it might have been the op not stating the question clearly.

Of course I could have given a wrong answer. It's happened before, and I'm quite sure it will happen again. I will always apologize when that happens. But isn't that how we learn? Well, at least it's 1 way to learn :D

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: Parsing SVG with an external program in Java

Postby hulf2012 » Sat Jul 18, 2015 10:50 pm

Homer Simpson mode:
Ough!

Totally wrong here
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

v1nce
Posts: 696
Joined: Wed Jan 13, 2010 4:36 am

Re: Parsing SVG with an external program in Java

Postby v1nce » Sun Jul 19, 2015 2:34 am

I'm afraid you will not find anything "easier" than batik.

What did you try ?

Did you succeed in displaying a svg into a java UI component ?
http://stackoverflow.com/questions/2611 ... with-batik

when you queried your path you should be able to cast it into a SVGPath
https://xmlgraphics.apache.org/batik/ja ... pport.html

using getPointAtLength you should be able to get the point at (distance) from the start of the path

then you could "insert" a circle at x,y into the document
https://xmlgraphics.apache.org/batik/using/dom-api.html

scruffydog
Posts: 13
Joined: Thu Nov 15, 2012 12:14 pm

Re: Parsing SVG with an external program in Java

Postby scruffydog » Sun Jul 19, 2015 8:54 am

Hi,

I did not realize that there were javascript libraries for SVG. I am going to try snap.svg. Thank you all for your suggestions and feedback.

-- Craig


Return to “SVG / XML Code”