[英]Unable to edit SVG using Batik in Java?
I have a student card SVG that has name, id and other field that I want to edit through Java, as the user inputs them using GUI. 我有一个学生卡SVG,其中有名称,ID和其他字段,我想通过Java编辑,因为用户使用GUI输入了它们。
I have successfully parsed the SVG using Batik but I can't see the changes that I made in SVG file when I open it. 我已经使用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();
}
When I print out one the of SVG element's value using 当我使用打印出SVG元素的值之一时
System.out.println(svg.getTextContent());
It's changed but when I open the SVG in notepad it's the same. 它已更改,但是当我在记事本中打开SVG时是相同的。
SVG SVG
<text x="759" y="361" id="name" class="fil3 fnt3">STUDENT</text>
UPDATE FOR OTHERS: Solved with 其他更新:解决了
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();
It looks like you aren't using any particular SVG features here, just some generic XML parsing. 看起来您在这里没有使用任何特定的SVG功能,只是一些常规XML解析。 The result of parsing the document with
createDocument
is a DOM in memory, but that doesn't automatically write out your changes to a file. 使用
createDocument
解析文档的结果是内存中有一个DOM,但这不会自动将您所做的更改写出到文件中。 You'll have to do that explicitly. 您必须明确地做到这一点。 Using the
org.apache.batik.svggen.XmlWriter
class is one of serializing. 使用
org.apache.batik.svggen.XmlWriter
类是序列化之一。 You'll need to open a file for writing, and pass the FileWriter
to it, along with the Document
node. 您需要打开一个文件进行写入,并将
FileWriter
以及Document
节点传递给该Document
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.