如何在Java/Android中解析SVG?

Rog*_*vis 3 java svg android

假设我在包含某处的资产中有一个.svg文件

<polygon id="collide" fill="none" points="45,14 0,79 3,87 18,92 87,90 98,84 96,66 59,14"/>
Run Code Online (Sandbox Code Playgroud)

您认为找到此多边形并将其点解析为Points []数组的最佳方法是什么?

谢谢!

Ale*_*lex 6

如上面的评论中所述,使用XML和XPath,可以轻松完成(无异常检查):

public static Point[] getPoints(String xml) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(xml)));
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//polygon[@id='collide']/@points");
    String[] pointsAttr = ((String) expr.evaluate(doc, XPathConstants.STRING)).split("\\p{Space}");
    Point[] points = new Point[pointsAttr.length];
    for (int i = 0; i < pointsAttr.length; ++i) {
        String[] coordinates = pointsAttr[i].split(",");
        points[i] = new Point(Integer.valueOf(coordinates[0]), Integer.valueOf(coordinates[1]));
    }
    return points;
}
Run Code Online (Sandbox Code Playgroud)

我不知道你在Android上拥有什么.