gpt4 book ai didi

java - 从 SVG 中提取图层

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:08 26 4
gpt4 key购买 nike

我当前正在加载一个 SVG 文件,并解析它以提取数据并确定有关文档的某些内容。我想显示的一件事是当前节点的层可追溯性

编辑:创建 SVG 文档

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(svg);

String xpathIDExp = "//g/@id";

XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
XPathExpression expression = xpath.compile(xpathIDExp);

svgIDPaths = (NodeList)expression.evaluate(doc, XPathConstants.NODESET);

..

private void printText(String output, String errorMsg) {

String parentName = new String();

for (int i = 0; i < svgIDs.getLength(); i++) {
Node node = svgIDs.item(i);
String name = node.getNodeValue();
if (node.getNodeValue().equals(output)) {
Node parent = node.getParentNode();
parentName = node.getParentNode().getNodeValue();

}
}
}

output 和 errorMsg 是我试图显示的内容(当前节点值和自定义错误消息)。我想将父节点添加到当前节点以显示图层。然而,对于我访问的每个节点,父节点都是空的。

有什么办法可以做到这一点吗?

此外,我还使用 xPath 和 Apache POI 来加载/解析 SVG。

编辑:父节点为空,并且我正在访问 SVG 的 ID 值,因此 getNodeValue() 应该是 ID 字段。

svgIds 是一个节点列表,其中包含基于 ID 字段使用 xpath 提取的所有节点。

编辑:SVG 文档

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" onload="initBlackBoard(evt); initClock(evt)"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="792px" height="612px"
viewBox="0 0 792 612" enable-background="new 0 0 792 612" xml:space="preserve">
<defs>

</defs>
<symbol id="SYMBOL_1" viewBox="-16.667 -16.666 33.333 33.333">
<polygon fill="#812C24" points="16.667,-16.666 -16.667,-16.666 -16.667,16.667 16.667,16.667 "/>
</symbol>
<symbol id="Symbol_2" viewBox="-23.333 -23.334 46.667 46.667">
<path fill="#09743B" d="M23.333,0c0-12.886-10.447-23.333-23.333-23.333c-12.886,0-23.333,10.447-23.333,23.333
c0,12.887,10.447,23.333,23.333,23.333C12.886,23.333,23.333,12.886,23.333,0z"/>
</symbol>
<g id="TEXT__x26__OUTLINE">
<text transform="matrix(1 0 0 1 43.9995 42.2339)"><tspan x="0" y="0" font-family="'MyriadPro-Regular'" font-size="12">Point names that are</tspan><tspan x="0" y="14.4" font-family="'MyriadPro-Regular'" font-size="12">linked to more than one</tspan><tspan x="0" y="28.8" font-family="'MyriadPro-Regular'" font-size="12">symbol</tspan></text>
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="19.555" y1="100.778" x2="772.889" y2="100.778"/>
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="185.111" y1="15.26" x2="185.111" y2="590.815"/>
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="394" y1="15.26" x2="394" y2="590.815"/>
<line fill="none" stroke="#000000" stroke-miterlimit="10" x1="602.889" y1="15.26" x2="602.889" y2="590.815"/>
<text transform="matrix(1 0 0 1 240.7773 49.4341)"><tspan x="0" y="0" font-family="'MyriadPro-Regular'" font-size="12">Analog points that are</tspan><tspan x="0" y="14.4" font-family="'MyriadPro-Regular'" font-size="12">linked to more than once</tspan></text>
<text transform="matrix(1 0 0 1 411.2217 56.6338)" font-family="'MyriadPro-Regular'" font-size="12">Linked points to compare to csv file</text>
<text transform="matrix(1 0 0 1 626.2217 35.0337)"><tspan x="0" y="0" font-family="'MyriadPro-Regular'" font-size="12">SVG interactivity is enabled</tspan><tspan x="0" y="14.4" font-family="'MyriadPro-Regular'" font-size="12">on this display and screen</tspan><tspan x="0" y="28.8" font-family="'MyriadPro-Regular'" font-size="12">shots of what the menu items </tspan><tspan x="0" y="43.2" font-family="'MyriadPro-Regular'" font-size="12">look like are provided below</tspan></text>
</g>
<g id="Layer_2">
<g id="SYMBOL_LINK-1">

<use xlink:href="#SYMBOL_1" width="33.333" height="33.333" x="-16.667" y="-16.666" transform="matrix(1 0 0 -1 60.666 141.8896)" overflow="visible"/>
</g>
<g id="Symbol_LINK-1">

<use xlink:href="#Symbol_2" width="46.667" height="46.667" x="-23.333" y="-23.334" transform="matrix(1 0 0 -1 60.666 205.2227)" overflow="visible"/>
</g>
<text transform="matrix(1 0 0 1 101.7783 145.668)" font-family="'MyriadPro-Regular'" font-size="12">Symbol 1</text>
<text transform="matrix(1 0 0 1 101.7783 207.335)" font-family="'MyriadPro-Regular'" font-size="12">Symbol 2</text>
<g id="Value_ANALOGPOINT">
<text transform="matrix(1 0 0 1 226.2222 133.001)" font-family="'MyriadPro-Regular'" font-size="12">XXXX</text>
</g>
<g id="Value_ANALOGPOINT_1_">
<text transform="matrix(1 0 0 1 226.2222 163.001)" font-family="'MyriadPro-Regular'" font-size="12">XXXX</text>
</g>
</g>
<g id="Layer_1">

<image overflow="visible" width="261" height="232" xlink:href="Screen Shot 2016-09-09 at 1.58.34 PM.png" transform="matrix(0.63 0 0 0.63 611.2217 224.1123)">
</image>

<image overflow="visible" width="500" height="329" xlink:href="Screen Shot 2016-09-09 at 1.58.41 PM.png" transform="matrix(0.3 0 0 0.3 618.4443 119.668)">
</image>

<image overflow="visible" width="239" height="216" xlink:href="Screen Shot 2016-09-09 at 2.12.18 PM.png" transform="matrix(0.7229 0 0 0.7229 607.0547 390.7773)">
</image>
<g id="Value_Hour">
<text transform="matrix(1 0 0 1 683.999 582.6289)" font-family="'MyriadPro-Regular'" font-size="12">XX</text>
</g>
<g id="Value_Minute">
<text transform="matrix(1 0 0 1 702.1074 583.0049)" font-family="'MyriadPro-Regular'" font-size="13.2778">XX</text>
</g>
<g id="Value_Second">
<text transform="matrix(1 0 0 1 721.7773 583.0059)" font-family="'MyriadPro-Regular'" font-size="13.2778">XX</text>
</g>
<text transform="matrix(1 0 0 1 637.332 583.0049)" font-family="'MyriadPro-Regular'" font-size="12">Clock</text>
</g>
</svg>

最佳答案

查找当前节点的父节点名称的函数(节点是节点的当前id)。现在我正在尝试找到该名称之上的每个父名称(循环)。如果有人有任何建议,将会有所帮助。

private String findParentName(String node) {

Node parentNode;
String parentName;
String nodeName = node;

try {
StringBuilder xpathIDExp = new StringBuilder();
xpathIDExp.append("//g[@id = '").append(nodeName).append("']/../@id");

XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
XPathExpression expression = xpath.compile(xpathIDExp.toString());

parentNode = (Node)expression.evaluate(svgDoc, XPathConstants.NODE);
parentName = parentNode.getNodeValue();

if (!parentName.isEmpty()) {
StringBuilder st = new StringBuilder();

st.append("'").append(parentName).append(" > ").append(nodeName).append("'");
nodeName = st.toString();
}
} catch (XPathExpressionException ex) {
Logger.getLogger(SVGParser.class.getName()).log(Level.SEVERE, null, ex);
}

return nodeName;
}

关于java - 从 SVG 中提取图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com