我正在尝试读取从旧程序导出的较大 (1 MB) svg 文件。我无权访问该程序或数据。我只有这个导出的 svg 文件,我可能需要定期从这个源导入一个新导出的 svg 文件。我发现使用蜡染的第一个问题是阅读此文件时非常严格。例如,Firefox 读取和显示此文件没有问题。蜡染(包括 Squiggle 无法显示,因为文档中有一些“自定义”标签)。我得到的例外是......
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: menu).
at org.apache.batik.dom.AbstractNode.createDOMException(AbstractNode.java:408)
at org.apache.batik.dom.svg.SVGDOMImplementation.createElementNS(SVGDOMImplementation.java:211)
at org.apache.batik.dom.svg.SVGOMDocument.createElementNS(SVGOMDocument.java:372)
at org.apache.batik.dom.util.SAXDocumentFactory.startElement(SAXDocumentFactory.java:625)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2756)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(SAXDocumentFactory.java:431)
at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(SAXDocumentFactory.java:349)
at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(SAXSVGDocumentFactory.java:200)
at com.samsix.nrg.io.SvgFileImporter.importFile(SvgFileImporter.java:74)
...因此,使用不完全有用的链接编写蜡染 Dom 扩展,我设法编写了以下内容...
public ImportReport importFile( final String uri )
throws
IOException
{
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
SVGDOMImplementation implementation = (SVGDOMImplementation) factory.getDOMImplementation( null );
implementation.registerCustomElementFactory( "http://www.w3.org/2000/svg",
"menu",
new ExtensibleDOMImplementation.ElementFactory() {
@Override
public Element create( final String prefix,
final Document doc )
{
System.out.println( "Element.create: " + prefix );
return new GenericElement( "prefix",
(org.apache.batik.dom.AbstractDocument) doc );
}
} );
SVGDocument doc = (SVGDocument) factory.createDocument( uri );
System.out.println( doc.getDocumentURI() );
}
但是我得到了同样的错误, System.out.println() 语句从未被命中,所以它显然没有正确注册我的工厂。附带说明一下,无论如何,是否有使 Batik 不那么严格,以便它像 Firefox 那样跳过它不理解的东西?