Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Java中使用Batik编辑SVG?_Java_Xml_Svg_Batik - Fatal编程技术网

无法在Java中使用Batik编辑SVG?

无法在Java中使用Batik编辑SVG?,java,xml,svg,batik,Java,Xml,Svg,Batik,我有一个学生卡SVG,其中包含姓名、id和其他字段,当用户使用GUI输入时,我希望通过Java编辑这些字段 我已经使用Batik成功地解析了SVG,但是在打开SVG文件时,我看不到在SVG文件中所做的更改 String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); String uri = "card.svg"

我有一个学生卡SVG,其中包含姓名、id和其他字段,当用户使用GUI输入时,我希望通过Java编辑这些字段

我已经使用Batik成功地解析了SVG,但是在打开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();
}
当我使用

System.out.printlnsvg.getTextContent

它已更改,但当我在记事本中打开SVG时,它是相同的

SVG


这里似乎没有使用任何特定的SVG功能,只是一些通用的XML解析。使用createDocument解析文档的结果是内存中有一个DOM,但它不会自动写出对文件的更改。你必须明确地这样做。您需要打开一个文件进行写入,并将FileWriter连同文档节点一起传递给它。

FileOutputWriter?对不起,FileWriter。我已经有一段时间没有使用Java了。我觉得你的更新2很不错。如果您仍然有问题,请在batik用户邮件列表中询问。
<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();