0

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 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();
}
teabot
  • 14,828
  • 9
  • 61
  • 78
Luke
  • 1
  • 1

1 Answers1

7

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"); 
teabot
  • 14,828
  • 9
  • 61
  • 78
  • Thanks it now works, it write the unicode sign in it.Great! If there is a solution how i could write thinks like XX from java in svg would interest me too. But mass thanks for that fast answer – Luke Oct 29 '09 at 11:37
  • 1
    Luke, you should **accept** the answer, to give teabot credit for it. – Jonathan Feinberg Oct 29 '09 at 13:38