Re: Unable to output DOM document into SVG/XML format

Thomas DeWeese <[email protected]>
Newsgroups gmane.text.xml.batik.user
Message-ID <[email protected]>
Hi Alan,

On 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?

   Yes and no respectively.  The calls to appendChild will modify 'doc' but the SVGGraphics2D doesn't stream out 'doc', it builds it's own DOM tree that it streams out (it uses the document you pass it as a factory for the elements in the Dom tree it builds).  You can get the tree it has written by calling getRoot() see it's JavaDoc for more info.

> 
>         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");

   As was mentioned already this should be 'createElementNS' with the SVG namespace.

>         svgRoot.appendChild(e);
>         
>         Text t = doc.createTextNode("Hello World");
>         
>         svgRoot.appendChild(t);

     You probably want to append this to 'e'.

     Regards,
     Thomas
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.