The SVG and CSS DOM Interfaces

When you first create an SVG Document from Java it will only support the DOM Core calls (basically traversal of the tree and accessing attributes).

The really interesting CSS and SVG specific DOM interfaces will not work properly until you initialize them. The code below is the simplest example of how to initialize them.

This defaults a lot of behaviour and in particular for real uses most people will want to provide a custom implementation of the UserAgent class.

If you are using the JSVGCanvas it does this when you associate an SVGDocument with the canvas. It will do this for you asynchronously you can be notified when it is done by registering a GVTTreeBuilderListener with the canvas before setting an SVGDocument (or URL) on the canvas.


import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.GVTBuilder;

import org.apache.batik.gvt.GraphicsNode;

/* ..... */
        SVGDocument    svgDoc;
        UserAgent      userAgent;
        DocumentLoader loader;
        BridgeContext  ctx;
        GVTBuilder     builder;
        GraphicsNode   rootGN;
        userAgent = new UserAgentAdapter();
        loader    = new DocumentLoader(userAgent);
        ctx       = new BridgeContext(userAgent, loader);
        builder   = new GVTBuilder();
        rootGN    =, svgDoc);
  • No labels