【发布时间】:2017-06-07 14:30:31
【问题描述】:
我有一张学生卡 SVG,其中包含我想通过 Java 编辑的名称、id 和其他字段,因为用户使用 GUI 输入它们。
我已经使用 Batik 成功解析了 SVG,但是我在打开 SVG 文件时看不到它所做的更改。
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();
}
当我使用
打印出 SVG 元素的值之一时System.out.println(svg.getTextContent());
它已经改变了,但是当我在记事本中打开 SVG 时,它是一样的。
SVG
<text x="759" y="361" id="name" class="fil3 fnt3">STUDENT</text>
其他人的更新:已解决
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();
【问题讨论】: