【问题标题】:Extract Layers from SVG从 SVG 中提取图层
【发布时间】:2017-02-07 23:32:39
【问题描述】:

我目前正在加载一个 SVG 文件,并对其进行解析以提取数据并确定有关文档的某些内容。我想展示的一件事是当前节点的Layer Traceability

编辑:创建 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 是一个节点列表,包含使用 xpath 基于 ID 字段提取的所有节点。

编辑: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>

【问题讨论】:

  • 考虑向我们展示您拥有哪种 SVG 以及您在哪里声明和初始化变量,例如 svgIDs。至于空值,父节点本身是空的还是只有getNodeValue()?在 W3C DOM 元素节点中总是有一个空节点值。您可能需要先说明您想要哪些数据,然后我们才能知道您需要访问哪个 DOM 方法或属性。
  • 我更新了它来回答你的问题。第二段代码在一个单独的类中,svgIDs 是对 svgIDPaths 的引用。重申父节点对象为空,对于每个节点。

标签: java apache xpath svg


【解决方案1】:

函数查找当前节点的父名称(节点是节点的当前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;
    }

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 2018-09-23
    • 2016-08-01
    • 2021-12-17
    • 2020-01-14
    • 2013-08-09
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多