我目前正在加载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();
}
}
}
输出和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>
答案 0 :(得分:0)
查找当前节点的父名称的功能(节点是节点的当前 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;
}