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

We generate SVG Document it looks fine, but when we try to work with it :

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

ER: Caused by: 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] 
...
...

BUT if we recreate svgDocument from String or file:

e.g.

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

or

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

There are no mistake. Could you help?

Thanks.

P.S. use element does exist but we have some weird bug at runtime

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"/>

Upvotes: 2

Views: 1644

Answers (1)

Robert Longson
Robert Longson

Reputation: 124219

You can't use setAttribute to set an attribute in the xlink namespace, it will only create attributes in the null namespace. What you need instead is setAttributeNS for xlink:href attributes.

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

Upvotes: 2

Related Questions