Search code examples
javasvgbatik

How to get SVGDocument object from an SVG string?


I gonna get an SVGDocument object to fill into JSVGCanvas, but I just had an SVG string without any files, so I cannot use URI to construct.


Solution

  • You can read your SVG from a StringReader like this:

    StringReader reader = new StringReader(svgString);
    String uri = "file:make-something-up";
    String parser = XMLResourceDescriptor.getXMLParserClassName();
    SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
    SVGDocument doc = f.createSVGDocument(uri, reader);
    

    You need to make up a valid URI but it's not important unless you make relative references to other URIs from your SVG.