Writing unicode characters with Batik doesn't work
I am writing a project with Batik, which is for multi-language image. Therefore I need signs like "sigma" or "alpha". I have to write the character as text - not as a polygon or as a glyph - because it has to be written by my project again.
If I write a unicode character in my SVGDocument it is shown correctly in the debugger, but if I write to SVG there is always a ?
, or for normal-Letter such as A
, ?A
as a result.
I think is a problem from my writer but I don't know how to fix it. I know that there are some solutions from SVG like using unicode with &#XXX
or σ
but I can't give the Node that String and it will written in the correct form.
Here is short and hopefully understandable code example:
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 Except开发者_如何转开发ion
{
/* 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();
}
Try writing the document with an OutputStreamWriter and specifying the character encoding explicitly to something that supports unicode:
OutputStream fileOutputStream = new FileOutputStream(svgFile);
Writer svgWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
精彩评论