I noticed a couple things, but am not where I can currently compile/test....
Create the TextElement in the SVG namespace
Change ...
Element e = doc.createElement("text");
...to...
Element e = doc.createElementNS(svgNS, "text");
... append the text to the textEl...
Change...
svgRoot.appendChild(t);
...to...
e.appendChild(t);
On Thu, Nov 24, 2011 at 9:16 PM, Alan Pandit <[email protected]> wrote:
> I am attempting to output the contents of a DOM Tree in XML format to
> System.out using the SVGGraphics2D class. Essentially, I want to be able
> to see what tags I am creating as I perform calls to appendChild(), but
> when I execute the code below, I don't see "Hello World" or <text> as I
> expect to see. Are the calls to appendChild() supposed to change the
> Document object named doc so the changes will be reflected in the streamed
> out SVG?
>
> String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
> DOMImplementation impl =
> SVGDOMImplementation.getDOMImplementation();
> Document doc = impl.createDocument(svgNS, "svg", null);
>
> Element svgRoot = doc.getDocumentElement();
>
> Element e = doc.createElement("text");
> svgRoot.appendChild(e);
>
> Text t = doc.createTextNode("Hello World");
>
> svgRoot.appendChild(t);
>
> // Create an instance of the SVG Generator.
> SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
> try{
>
>
> // Finally, stream out SVG to the standard output using
> // UTF-8 encoding.
> boolean useCSS = true; // we want to use CSS style attributes
> Writer out = new OutputStreamWriter(System.out, "UTF-8");
> svgGenerator.stream(out, useCSS);
> }
> catch(Exception exc) {
>
> System.out.println(exc+"");
> }
>
>
> This is the OUTPUT I get:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
> 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
> <svg style="stroke-dasharray:none; shape-rendering:auto;
> font-family:'Dialog'; text-rendering:auto; fill-opacity:1;
> color-interpolation:auto; color-rendering:auto; font-size:12; fill:black;
> stroke:black; image-rendering:auto; stroke-miterlimit:10;
> stroke-linecap:square; stroke-linejoin:miter; font-style:normal;
> stroke-width:1; stroke-dashoffset:0; font-weight:normal; stroke-opacity:1;"
> xmlns="http://www.w3.org/2000/svg" contentScriptType="text/ecmascript"
> preserveAspectRatio="xMidYMid meet" xmlns:xlink="
> http://www.w3.org/1999/xlink" zoomAndPan="magnify" version="1.0"
> contentStyleType="text/css"
> ><!--Generated by the Batik Graphics2D SVG Generator--><defs
> id="genericDefs"/><g/></svg>
>
>
> Thanks.
>
> Alan
>