Lloyd Smith Lloyd Smith - 1 month ago 10
Apache Configuration Question

Extract Layers from SVG

I am currently loading an SVG file, and parsing it to extract the data and determine certain things about the document. One thing I would like to display, is the Layer Traceability of the current Node.

Edit: Creating SVG document

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 and errorMsg are what I am trying to display (current node value and custom error message). I want to add the parent node to the current node to display the layers. However the parent node is null for every node I access.

Is there any way to do this?

Also I'm using xPath and Apache POI for loading/parsing SVG.

Edit: The parent node is null, and I am accessing the ID values of the SVG, so the getNodeValue() should be the ID field.

svgIds is a nodelist containing all the nodes extracted using xpath based off the ID field.

Edit: SVG Doc

<?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>

Answer

Function to find the parent name of the curent node (node is the current id of the node). Now I'm trying to find every parent name above this one as well (loop). If anyone has any suggestions that would be helpful.

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;
    }
Comments