无法在Java中使用Batik编辑SVG? [英] Unable to edit SVG using Batik in Java?

查看:847
本文介绍了无法在Java中使用Batik编辑SVG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学生卡SVG,它具有我想通过Java编辑的名称,ID和其他字段,因为用户使用GUI输入它们。

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.

我已经使用Batik成功解析了SVG,但是当我打开它时,我看不到我在SVG文件中所做的更改。

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.

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();
}

当我使用

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

它已经改变但是当我在记事本中打开SVG时它是一样的。

It's changed but when I open the SVG in notepad it's the same.

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();


推荐答案

看起来你没有使用任何特定的SVG这里的功能,只是一些通用的XML解析。使用 createDocument 解析文档的结果是内存中的DOM,但不会自动将更改写入文件。你必须明确地这样做。 使用 org.apache.batik.svggen.XmlWriter class是序列化之一。您需要打开一个文件进行编写,然后将 FileWriter 传递给它,以及文档节点。

It looks like you aren't using any particular SVG features here, just some generic XML parsing. 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. You'll have to do that explicitly. Using the org.apache.batik.svggen.XmlWriter class is one of serializing. You'll need to open a file for writing, and pass the FileWriter to it, along with the Document node.

这篇关于无法在Java中使用Batik编辑SVG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆