1

im trying to extract only the coordinates from a "svg image", that's in a String, unsuccessful. I've read many question in the forum with similar problems, but anyone helped me.

So, I have this svg xmlns in a String and I want just the values from the "path".

This is what I got so far:

    public class GetPathCoords {

    public static void main(String[] args) throws Exception {

        String xmlsvg = "<?xml version="1.0" encoding="UTF-8" standalone="no"?>
                         <svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" height="416" width="800">
                            <g stroke-linejoin="round" stroke-linecap="round" fill="none" stroke="black">
                               <path stroke-width="6" d="M399,301c0,0 0,0 0,0 "/>
                               <path stroke-width="7" d="M399,301c0,0 0,1 0,1 0,-3 0,-4 0,-7 "/>
                               <path stroke-width="5" d="M399,295c0,-25 0,-25 0,-50 "/>
                               <path stroke-width="3" d="M399,245c0,-25 0,-25 0,-50 0,-21 0,-21 0,-43 0,-13 0,-13 0,-26 "/>
                               <path stroke-width="4" d="M399,126c0,-2 0,-2 0,-5 "/>
                            </g>
                         </svg>"

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        byte[] xmlDATA = xmlsvg.getBytes("UTF-8");
        ByteArrayInputStream in = new ByteArrayInputStream(xmlDATA);
        Document doc = dBuilder.parse(in);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        String xpathQuery = "/svg/g/path";
        XPathExpression expr = xpath.compile(xpathQuery);
        Node svgNode = (Node) expr.evaluate(doc, XPathConstants.NODE);

        System.out.println(svgNode);
    }
}

There may be an error with the string, if you only copy n paste, but its a valid string on my IDE, I just added some lines and spaces here for better understanding.

Original string:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" height="416" width="800"><g stroke-linejoin="round" stroke-linecap="round" fill="none" stroke="black"><path stroke-width="6" d="M399,301c0,0 0,0 0,0 "/><path stroke-width="7" d="M399,301c0,0 0,1 0,1 0,-3 0,-4 0,-7 "/><path stroke-width="5" d="M399,295c0,-25 0,-25 0,-50 "/><path stroke-width="3" d="M399,245c0,-25 0,-25 0,-50 0,-21 0,-21 0,-43 0,-13 0,-13 0,-26 "/><path stroke-width="4" d="M399,126c0,-2 0,-2 0,-5 "/></g></svg>

I would like to get all coords from path ignoring that stroke-width and the first two coma separated alpha-numbers of "d".

Any help will be appreciated, thank you all.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • `//@d`with a loop and one tidy text command to strip comas would suffice. On liner solution with XPath would be :`concat(substring-after(string((//@d)[1]),'c0,'),"|",substring-after(string((//@d)[2]),'c0,'),"|",substring-after(string((//@d)[3]),'c0,'),"|",substring-after(string((//@d)[4]),'c0,'),"|",substring-after(string((//@d)[5]),'c0,'))`. – E.Wiest Mar 12 '20 at 02:43

0 Answers0