xml - org.apache.batik.bridge.BridgeException : null:-1 The attribute "xlink:href" of the element <use> is required

标签 xml apache svg element batik

我们生成 SVG 文档,它看起来不错,但是当我们尝试使用它时:

UserAgent userAgent = new UserAgentAdapter();
BridgeContext ctx = new BridgeContext(userAgent);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
builder.build(ctx, svgDocument);

ER:引起:org.apache.batik.bridge.BridgeException:null:-1

The attribute "xlink:href" of the element <use> is required 
at org.apache.batik.bridge.SVGUseElementBridge.buildCompositeGraphicsNode(SVGUseElementBridge.java:120) [%HOME%/lib/batik-all_new.jar:1.8pre+r1435044] 
at org.apache.batik.bridge.SVGUseElementBridge.createGraphicsNode(SVGUseElementBridge.java:98) [%HOME%/lib/batik-all_new.jar:1.8pre+r1435044] 
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:213) [%HOME%/lib/batik-all_new.jar:1.8pre+r1435044] 
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:171) [%HOME%/lib/batik-all_new.jar:1.8pre+r1435044] 
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:219) [%HOME%/lib/batik-all_new.jar:1.8pre+r1435044] 
...
...

但是如果我们从字符串或文件重新创建 svgDocument:

例如

SVGDocument svg = factory.createSVGDocument("svg", new ByteArrayInputStream(svgString.getBytes()));

SVGDocument svg = factory.createSVGDocument("svg", new FileInputStream(new File("C:\\file.xml")));

没有错误。你能帮忙吗?

谢谢。

附注use 元素确实存在,但我们在运行时遇到一些奇怪的错误

SVGUseElement svg = (SVGUseElement)svgElement.getOwnerDocument()
    .createElementNS("http://www.w3.org/2000/svg", "use");
svg.setAttribute("xlink:href", "#" + deviceObjectParameters.pictureURL.hashCode());
<use xlink:actuate="onLoad" xlink:type="simple" xlink:show="embed" transform="translate(0.0, 0.0) rotate(0, 0, 0) scale(1.0, 1.0)" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#1232024040"/>

最佳答案

您不能使用 setAttribute 在 xlink 命名空间中设置属性,它只会在 null 命名空间中创建属性。您需要的是 xlink:href 属性的 setAttributeNS。

svg.setAttributeNS("http://www.w3.org/1999/xlink", "href", "#" + deviceObjectParameters.pictureURL.hashCode());

关于xml - org.apache.batik.bridge.BridgeException : null:-1 The attribute "xlink:href" of the element <use> is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39789665/

相关文章:

xml - Eclipse Kepler 不自动生成 web.xml

xml - 如何在不使用 SQL Server 中的 XQuery 对元素名称进行硬编码的情况下从 XML 中提取值?

javascript - 当我删除一个矩形时,我怎样才能同时删除它的字母

java - 设置为 View.VISIBLE 后,Android Button 需要点击几次才能工作

从模型加载 XML 时出现 Javascript 意外标记 '<'

apache - 使用 apache 和 mod_rewrite 进行 A/B 测试

apache - CONNECT 请求通过 SSL 连接转发 HTTP 代理?

.net - 在 Ubuntu 和 Certbot 上使用 SSL 的 Blazor

html - 如何使用 SVG 路径创建窗帘效果?

javascript - 使用 javascript 填充 svg 内的图案