Java 转换SVG时Batik出错:元素的属性“xlink:href”是必需的
我有一个SVG文件,结构如下:Java 转换SVG时Batik出错:元素的属性“xlink:href”是必需的,java,svg,batik,Java,Svg,Batik,我有一个SVG文件,结构如下: <svg xmlns="http://www.w3.org/2000/svg" width="506px" height="261px" viewBox="0 0 506 261" style="overflow: hidden; display: block; width: 506px; height: 261px;"> <defs> <path fill="none" stroke="rgb(211,211,21
<svg xmlns="http://www.w3.org/2000/svg" width="506px" height="261px" viewBox="0 0 506 261" style="overflow: hidden; display: block; width: 506px; height: 261px;">
<defs>
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_0" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_1" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_2" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_3" />
<path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_4" />
</defs>
<g style="pointer-events:visiblePainted" transform="translate(-57.822952932363535 -41.05614567526554)" image-rendering="auto" shape-rendering="auto">
<g>
<path fill="none" stroke="rgb(211,211,211)" d="M 257.9119715596548,73.05614567526554 L 257.9119715596548,135.06828972638465" stroke-opacity="1" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" />
<use href="#ygc10_0" transform="matrix(0 -2 2 0 257.912 135.068)" style="pointer-events: none;" />
</g>
<g>
<path fill="none" stroke="rgb(211,211,211)" d="M 107.82295293236353,207.4377203121161 L 107.82295293236353,261.03014731718906" stroke-opacity="1" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" />
<use href="#ygc10_1" transform="matrix(0 -2 2 0 107.823 261.03)" style="pointer-events: none;" />
</g>
...
但是,我在转码的调用中遇到了一个异常:
org.apache.batik.bridge.BridgeException: null:-1
The attribute "xlink:href" of the element <use> is required
at org.apache.batik.bridge.SVGUseElementBridge.buildCompositeGraphicsNode(Unknown Source)
at org.apache.batik.bridge.SVGUseElementBridge.createGraphicsNode(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
...
但没有效果。谢谢大家。您的SVG文件无效,解决方法之一是在根SVG元素上添加xmlns:xlink属性,如下所示
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="506px" height="261px" viewBox="0 0 506 261">
正如所指出的,您还需要将href属性更改为xlink:href,因为href是SVG 2,Batik还不支持该属性。信息:在我的例子中,生成SVG字符串没有宽度值。所以它抛出了异常
<g transform="translate(�, 85.00) scale(�)">
应该是
<g transform="translate(168.88, 85.00) scale(0.36)">
@Andrea和更新你的元素。更改href=。。。到xlink:href=。。。。xlink:part在SVG2中只是可选的,Batik还不支持SVG2。谢谢你们,Robert和Paul。现在,我已经能够尝试罗伯特的建议。我得到了另一个错误1字节UTF-8序列的无效字节1,我想我需要调查最初使用的字符集。顺便说一句,我很快也会换掉所有的衣服。现在非常感谢你们。再次非常感谢你们两位。添加xlink和SVG兼容性修复了它。
<g transform="translate(�, 85.00) scale(�)">
<g transform="translate(168.88, 85.00) scale(0.36)">