【发布时间】:2015-01-05 16:26:32
【问题描述】:
通常使用 Dom4J 从 XML 文件中获取属性值是没有问题的
在 XML 工具中,我也可以使用 xsl 和 Xpath 从该 SVG 中获取“使用填充”的值
<svg xmlns="http://www.w3.org/2000/svg" width="2250" height="2700" viewBox="0 0 1800 2160" color-interpolation-filters="sRGB" fill="none" overflow="visible" fill-rule="evenodd" stroke-linecap="square" stroke-miterlimit="3" xmlns:xlink="http://www.w3.org/1999/xlink">
<g transform="translate(363.17-1988.15)">
<use fill="#f00" xlink:href="#1"/>
<g transform="translate(72.59-8.504)">
<use xlink:href="#9"/>
<use xlink:href="#B"/>
<use xlink:href="#A"/>
</g>
<text x="25.29" y="2143.97" fill="#000" font-family="Arial" font-size="8">SetAnnotations</text>
</g>
例如在 xmlSpy 中工作
/svg/g/use/@fill 或 //g[text="SetAnnotations"]/use/@fill
这(以及我尝试过的所有其他方法)在 Java 中都失败了(通常有效)
public static String getNodeValue(Element doc, String xpath){
try{
return doc.selectSingleNode(xpath).getText();
}catch (Exception ex){
log.debug("getNodeValue failed with " + ex.getMessage());
return null;
}
}
或
public static String getAttributeValue(Element doc, String xpath, String attributeName){
try{
Node node = doc.selectSingleNode(xpath);
String value = node.valueOf(attributeName);
return value;
}catch (Exception ex){
log.debug("getAttributeValue failed with " + ex.getMessage());
return null;
}
}
由于某些原因,我只能使用 1.5 + dom4J,但由于 svg 已经很老了,我希望有一个没有像蜡染这样的额外库的解决方案
知道如何在 Java 中正确使用 svg(读取和更改属性)吗?
提前感谢任何提示
【问题讨论】: