如何在SVGDocument中放置SVG字符串

时间:2015-06-05 13:42:07

标签: java string svg

此代码没问题

import org.w3c.dom.svg.SVGDocument;
public static final String resource = "city.svg";
protected SAXSVGDocumentFactory factory;
String parser = XMLResourceDescriptor.getXMLParserClassName();
factory = new SAXSVGDocumentFactory(parser);
SVGDocument city = factory.createSVGDocument(new File(resource).toURL().toString());

我需要在SVGDocument中放置一个SVG字符串:

import org.w3c.dom.svg.SVGDocument;
String svgstring = "<svg>......<svg>";
protected SAXSVGDocumentFactory factory;
String parser = XMLResourceDescriptor.getXMLParserClassName();
factory = new SAXSVGDocumentFactory(parser);
SVGDocument city = ????? svgstring ??????;

1 个答案:

答案 0 :(得分:0)

蜡染动物:1.12

import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.svg.SVGDocument;

import java.io.IOException;
import java.io.StringReader;

public class Test {
    public static void main(String[] args) throws IOException {
       String svgString =  "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<svg version=\"1.1\" id=\"main\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n" +
                "\t width=\"3300px\" height=\"2400px\" viewBox=\"0 0 3300 2400\" style=\"enable-background:new 0 0 3300 2400;\" xml:space=\"preserve\">\n" +
                "\n" +
                "</svg>\n";

       SAXSVGDocumentFactory factory;
       String parser = XMLResourceDescriptor.getXMLParserClassName();
       factory = new SAXSVGDocumentFactory(parser);
       SVGDocument city = factory.createSVGDocument(null, new StringReader(svgString));
    }
}