blob: b6ed18bfd418dd6f255355f455764ccba13d77f5 [file] [log] [blame]
Title: SVG DOM API
#SVG DOM API
The [Document Object Model](http://www.w3.org/dom/) (DOM) is an API for XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated. This page shows how to create an SVG document using the DOM API.
## Getting started { #gettingStarted}
The DOM API defines an interface called [DOMImplementation](http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/DOMImplementation.html), which represents the bootstrap of any DOM implementation. The role of this class is to bootstrap a particular implementation of the DOM by providing a method to create a [Document](http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Document.html). Then, the concrete `Document` represents an XML document and also acts like a factory for the various DOM objects such as [Element](http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Element.html), [Attr](http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Attr.html) and [Text](http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Text.html).
How to get an instance of the `DOMImplementation` interface depends on the DOM implementation you are using. In Batik, the DOM implementation is located in the package `org.apache.batik.dom.svg` and the class is named [SVGDOMImplementation](http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/dom/svg/SVGDOMImplementation.html). The following example shows how to get a concrete `DOMImplementation` object.
:::java
import org.w3c.dom.DOMImplementation;
import org.apache.batik.dom.svg.SVGDOMImplementation;
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
Once you have an instance of a `DOMImplementation`, you are not relying on Batik-specific code any more and ready to use the DOM API.
## Creating a Document { #creating}
Using the `DOMImplementation`, you are now able to create a `Document`. The following example illustrates how to create an SVG document. Note that the Batiks DOM implementation can be used to represent either an SVG document fragment or any kind of XML document. Note that by choosing the namespace URI and the local name of the root element of SVG, we are creating an SVG document.
:::java
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.Document;
// We are using a constant available on the SVGDOMImplementation,
// but we could have used "http://www.w3.org/2000/svg".
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
Document doc = impl.createDocument(svgNS, "svg", null);
As we have created an SVG `Document`, we can cast this document to an [SVGDocument](../javadoc/org/w3c/dom/svg/SVGDocument.html) (defined in the `org.w3c.dom.svg` package) if needed.
## Building an SVG Document { #buildsvgdoc}
Finally, using the `Document` object, we are now able to construct SVG content. Note that the document created before supports both generic XML and SVG. Though the DOM implementation of Batik is an SVG DOM implementation, the SVG-specific methods that rely on the document having been rendered (particularly geometry related methods, such as [SVGLocatable.getBBox](../javadoc/org/w3c/dom/svg/SVGLocatable.html#getBBox())) cannot be used at this point.
The document can be built using DOM Level 2 Core methods. The following example shows how to create a red rectangle located at (10, 20), with a size of (100, 50) placed in a (400, 450) SVG canvas:
:::java
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = impl.createDocument(svgNS, "svg", null);
// Get the root element (the 'svg' element).
Element svgRoot = doc.getDocumentElement();
// Set the width and height attributes on the root 'svg' element.
svgRoot.setAttributeNS(null, "width", "400");
svgRoot.setAttributeNS(null, "height", "450");
// Create the rectangle.
Element rectangle = doc.createElementNS(svgNS, "rect");
rectangle.setAttributeNS(null, "x", "10");
rectangle.setAttributeNS(null, "y", "20");
rectangle.setAttributeNS(null, "width", "100");
rectangle.setAttributeNS(null, "height", "50");
rectangle.setAttributeNS(null, "fill", "red");
// Attach the rectangle to the root 'svg' element.
svgRoot.appendChild(rectangle);
The example given constructs a document equivalent to parsing the following SVG file:
:::xml
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="450">
<rect x="10" y="20" width="100" height="50" fill="red"/>
</svg>
# Creating a Document from an SVG file { #createdocfromsvgfile}
With Batik, you can also create an SVG DOM tree from a URI, an [InputStream](http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html), or a [Reader](http://java.sun.com/j2se/1.5.0/docs/api/java/io/Reader.html), using the [SAXSVGDocumentFactory](../javadoc/org/apache/batik/dom/util/SAXDocumentFactory.html). The following example illustrates how to create an SVG document from a URI using the `SAXSVGDocumentFactory` class.
:::java
import java.io.IOException;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
try {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
String uri = "http://www.example.org/diagram.svg";
Document doc = f.createDocument(uri);
} catch (IOException ex) {
// ...
}
As we have created an SVG `Document`, we can cast this document to an `SVGDocument` (defined in the `org.w3c.dom.svg` package) if needed.
## Rendering an SVG Document { #rendering}
Batik provides several ways to use an SVG DOM tree. Two modules can be immediately used to render your SVG document.
JSVGCanvas
:
The [JSVGCanvas](../javadoc/org/apache/batik/swing/JSVGCanvas.html) is a Swing component that can display SVG document. A SVG document can be specified using a URI or an SVG DOM tree (using the [setSVGDocument](../javadoc/org/apache/batik/swing/JSVGCanvas.html#setSVGDocument(Document)) method). For futher information about the `JSVGCanvas`, see the [Swing components module documentation](../using/swing.html).
ImageTranscoder
: The [ImageTranscoder](../javadoc/org/apache/batik/transcoder/image/ImageTranscoder.html) is a transcoder that can take a URI, an `InputStream` or an SVG DOM tree and produces a raster image (such JPEG, PNG or TIFF). By creating a [TranscoderInput](../javadoc/org/apache/batik/transcoder/TranscoderInput.html) object with the SVG DOM tree, you will be able to transform your SVG content to a raster image. For futher information, see the [transcoder module documentation](../using/transcoder.html).