You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Jan Willem Lokin <jw...@yahoo.com> on 2007/10/24 12:08:01 UTC

How to get an image into an SVG document?

Hi,

Below is some code that I'm trying to use to get an image into a SVG document. If I subsequently render the resulting image on a JSVGCanvas, I get an exception:

org.apache.batik.bridge.BridgeException: null:-1
The attribute "xlink:href" of the element <image> is required
    at org.apache.batik.bridge.SVGImageElementBridge.buildImageGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.SVGImageElementBridge.createGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
    at org.apache.batik.bridge.GVTBuilder.build(Unknown Source)
    at org.apache.batik.swing.svg.GVTTreeBuilder.run(Unknown Source) 

However, if I serialize to document to a file, it will render just fine if I read in the file and render it.

I must have missed something somewhere, but what?

-+- Jan Willem Lokin -+-




The code:

package svg;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class SVGPicture {

    private final int height;
    private final int width;
    private final Element svgRoot;
    private final Document document;
    private static final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

    public SVGPicture(int width, int height) {
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        document = impl.createDocument(svgNS, "svg", null);
        svgRoot = document.getDocumentElement();

        // Set the width and height attributes on the root 'svg' element.
        svgRoot.setAttributeNS(null, "width", Integer.toString(width));
        svgRoot.setAttributeNS(null, "height", Integer.toString(height));

        this.height = height;
        this.width = width;
    }

    public Document getDocument() {
        return document;
    }

    public void image(String fileName) {
        Element image = document.createElementNS(svgNS, "image");
        image.setAttributeNS(null, "width", Integer.toString(width));
        image.setAttributeNS(null, "height", Integer.toString(height));
        image.setAttributeNS(null, "xlink:href", fileName);
        svgRoot.appendChild(image);
    }
}





__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: How to get an image into an SVG document?

Posted by Cameron McCormack <ca...@mcc.id.au>.
Hi Jan.

Jan Willem Lokin:
> I must have missed something somewhere, but what?
…
>         image.setAttributeNS(null, "xlink:href", fileName);

This should be:

  image.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", fileName);

For reasoning see here:

  http://jwatt.org/svg/authoring/#namespace-aware-methods

-- 
Cameron McCormack, http://mcc.id.au/
	xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org