簡體   English   中英

無法在Java中使用蠟染來編輯SVG?

[英]Unable to edit SVG using Batik in Java?

我有一個學生卡SVG,其中有名稱,ID和其他字段,我想通過Java編輯,因為用戶使用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();

看起來您在這里沒有使用任何特定的SVG功能,只是一些常規XML解析。 使用createDocument解析文檔的結果是內存中有一個DOM,但這不會自動將您所做的更改寫出到文件中。 您必須明確地做到這一點。 使用org.apache.batik.svggen.XmlWriter類是序列化之一。 您需要打開一個文件進行寫入,並將FileWriter以及Document節點傳遞給該Document

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM