Loading...
Logo
Processing Forum
Hi there,

I have SVG code (XML) in a String, which I'd like to load into a PShapeSVG.  LoadShape seems to only work from a file.

Any ideas?  Thanks.

Replies(8)

PShapeSVG theShape = new PShapeSVG(new XMLElement(theString));
 
hmmm... I get this error:

Copy code
  1. Exception in thread "Animation Thread" java.lang.RuntimeException: root is not <svg>, it's <<?xml version="1.0" encoding="utf-8"?>
  2. <!-- Generator: Adobe Illustrator 15.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
  3. <<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
  4. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"...

Does loadShape() do any fancy conversion or what, to tell PShape that it's handling SVG?  I tried deleting everything before the doctype, but that didn't work either.
Remove everything before <svg, perhaps.
thanks for your help.

It still has the same error, however...  am I supposed to convert the SVG String into something else?

It seems when I set the content of the XMLElement, p5 adds on a couple of extra characters.

this...
Copy code
  1. XMLElement svg = new XMLElement(shapeString);
  2. System.out.println(svg);
produces...
Copy code
  1. <<svg version="1.1"...
  2. </svg>
  3. />
Any idea why the extra " <... />"?  Those aren't in my SVG file, or my String... XMLElement puts them there.  I guess I can try to kluge with some string manipulation, but that seems awfully messy

Where the new XMLElement(xmlString) comes from? (just curious). Another person has a similar issue. In current version of code, you have to use XMLElement.parse(xmlString) instead.
from 1.2.1, wasn't aware that constructor had vanished in 1.5, used to work fine, sorry for the misdirection
 
ugh, hope i never have to revisit several sketches that use that constructor all over the place!
 
[rant]if it's a public constructor, and beta is over, shouldn't it persist through minor versions at least?[/rant]
 
ok, getting warmer... this code at least does not throw errors:

Copy code
  1. XMLElement svg = new XMLElement();
  2. svg.parse(shapeString);
  3. System.out.println("the content is: " + svg.getContent());
  4. PShapeSVG shape = new PShapeSVG(svg);
but the println() reveals that the svg does not contain content, so I get a NullPointerException later on.  Any idea?

Re-read my answer and use the code I show...