Can't edit SVG using Batik in Java?

I have a student SVG that has a name, id, and another field that I want to edit through Java, as the user enters them using the GUI.

I successfully parsed SVG using Batik, but I don’t see the changes I made in the SVG file when I open it.

String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); String uri = "card.svg"; try { Document doc = f.createDocument(uri); NodeList nodeList = doc.getChildNodes(); Element svg = doc.getElementById("name"); svg.setTextContent("Your Name"); System.out.println(svg.getTextContent()); } catch (IOException e) { e.printStackTrace(); } 

When I print a single SVG element value using

System.out.println(svg.getTextContent());

It has changed, but when I open SVG in notepad, it is the same.

Svg

 <text x="759" y="361" id="name" class="fil3 fnt3">STUDENT</text> 

UPDATE FOR OTHERS: Baxin

 File file = new File("new.svg"); FileWriter fWriter = new FileWriter(file); XmlWriter.writeXml(svg, fWriter, false); // Most crucial part, It wasn't working just because of flush fWriter.close(); 
0
java xml svg batik
Jan 23 '17 at 6:17
source share
1 answer

It looks like you are not using any SVG features here, but only some general XML parsing. The result of parsing a document using createDocument is a DOM in memory, but it does not automatically write your changes to a file. You will have to do it explicitly. Using the org.apache.batik.svggen.XmlWriter class is serialization. You will need to open the file for writing and pass it the FileWriter , along with the Document node.

+1
Jan 26 '17 at 3:58 on
source share



All Articles