3

How can I set an attribute (such as "id") on SVG elements created with SVGGraphics2D? For example: I am creating a Polygon with the following code:

i've already tried the following but it does not work:

    public static final String SVG_NS = "http://www.w3.org/2000/svg";

                // Test: add id attribute to all SVG elements

        Element root = document.getDocumentElement();
        Node rootNode = (Node)g2d.getRoot(root);

        NodeList nl = rootNode.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n instanceof GenericElementNS) {
                Element el = (Element)n;

                el.setAttributeNS(SVG_NS, "id", "P001");
            }
        }

while there are no errors no attributes are generated in the output!

// Code to generate a polygon using SVGGraphics2D
        Polygon p = new Polygon();

        p.addPoint(551, 194);
        p.addPoint(569, 194);
        p.addPoint(569, 250);
        p.addPoint(551, 250);


        g2d.drawPolygon(p);

           // Here I want to set the attribute 'id="P001"' to the above element```
SBKDeveloper
  • 613
  • 1
  • 9
  • 20

0 Answers0