首页 > 解决方案 > 如何使用蜡染将 svg 解析为 Document?

问题描述

我的 svg 喜欢这个:

<svg xmlns="http://www.w3.org/2000/svg" 
xmlns:xlink="http://www.w3.org/1999/xlink">
...
</svg>

不止一个xmlns,怎么定义xmlnsbatik?以及如何解析为Document

String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
File file = new File(svgURI);
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document doc = f.createDocument(svgNS, "svg", file.toURI().toString());

上面的代码在运行时有异常:

Exception in thread "main" org.w3c.dom.DOMException: The current document is unable to create an element of the requested type (namespace: http://www.w3.org/2000/svg, name: usrs).
    at org.apache.batik.dom.AbstractNode.createDOMException(AbstractNode.java:407)
    at org.apache.batik.anim.dom.SVGDOMImplementation.createElementNS(SVGDOMImplementation.java:202)
    at org.apache.batik.anim.dom.SVGOMDocument.createElementNS(SVGOMDocument.java:373)
``

标签: batik

解决方案


DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(uri);

推荐阅读