我正在和Batik一起编写一个项目,用于多语言图像.因此我需要像"sigma"或"alpha"这样的标志.我必须将字符写为文本 - 而不是多边形或字形 - 因为它必须再次由我的项目编写.
如果我在SVGDocument中编写一个unicode字符,它会在调试器中正确显示,但是如果我写入SVG,则总是有一个?或者正常字母,例如A,?A结果.
我认为这是我作家的一个问题,但我不知道如何解决它.我知道SVG有一些解决方案,比如使用unicode &#XXX或者σ我不能给Node那个字符串,它会以正确的形式写出来.
这是简短且希望可以理解的代码示例:
enter code here
import java.io.File;
import java.io.FileWriter;
import java.net.URI;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Text;
public static void main(String args[]) throws Exception
{
/* Read Document
*/
URI source = new URI("file:D:/foo.svg");
//If there is no Parser:'parser' = null
String parser = XMLResourceDescriptor.getXMLParserClassName();
//for right interpretation
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
String sourceUri = source.toString();
/* add Textnode
*/
Document doc = f.createSVGDocument(sourceUri);
String textWithUni = "\u0041";
Text textNode = doc.createTextNode(textWithUni);
doc.appendChild(textNode);
/*write
*/
File output = new File("newFoo.svg");
FileWriter fw = new FileWriter(output);
DOMUtilities.writeDocument(doc, fw);
fw.flush();
fw.close();
}
Run Code Online (Sandbox Code Playgroud)
尝试使用OutputStreamWriter编写文档,并明确指定支持unicode的字符编码:
OutputStream fileOutputStream = new FileOutputStream(svgFile);
Writer svgWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1653 次 |
| 最近记录: |