How to convert SVG String to SVGDocument ?

August 23rd, 2007 | by Tonny Kohar |

Just simply use StringReader to wrap your string and pass it to DocumentFactory as the example below

Note: this sample utilize Apache Batik

String yourString="<svg> ... </svg>";
StringReader reader = new StringReader(yourString);
String uri = "set the document URI, so Batik will be able to resolve relative path";
try {
	String parser = XMLResourceDescriptor.getXMLParserClassName();
	SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
	doc = f.createSVGDocument(uri,reader);
} catch (Exception ex) {
	// do your error handling here
} finally {

Tags: , ,

  1. 2 Responses to “How to convert SVG String to SVGDocument ?”

  2. By jose alvarez on Feb 21, 2013 | Reply

    the variable doc in “doc = f.createSVGDocument(uri,reader);” where does it comes from?


  3. By Tonny Kohar on Feb 22, 2013 | Reply

    The variable doc is just simple SVGDocument eg:
    SVGDocument doc = f.createSVGDocument(uri,reader);

You must be logged in to post a comment.