Как я могу установить атрибут (например, "id") для элементов SVG, созданных с помощью SVGGraphics2D? Например: я создаю полигон со следующим кодом:
Я уже пробовал следующее, но оно не работает:
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");
}
}
пока ошибок нет, атрибуты не генерируются в выводе!
// 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```