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 KE Gan <kh...@gmail.com> on 2005/03/07 18:09:11 UTC

Navigating SVG DOM

Hi,

I want to manipulate a SVG document (loaded from a file) using Batik's
SVG DOM api (org.apache.batik.dom.svg).


(1) The sample in Batik's website show that to get a SVGDocument
instance, I have to cast from a org.w3c.dom.Document, which in turn I
obtain from SAXSVGDocumentFactory.

String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
InputStream is = new FileInputStream("svgdom\\resource\\sample.svg");
Document doc = f.createDocument(null, is);
SVGDocument svgDoc = (SVGDocument)doc;

Is this the most appropriate way ? Can I load a SVG file (into
SVGDocument) without using org.w3c.dom.Document as the medium ?


(2) To navigate the SVG DOM, I started by calling
SVGDocument.getRootElement(), which return a SVGSVGElement. To get the
list of children nodes from this root, I use the inherited
org.w3c.dom.Node.getChildNodes().

NodeList nodeList = svgDoc.getRootElement().getChildNodes();

Is this the right way go get a list of children nodes ? Is there a SVG
DOM specific API for getting the list of children nodes ? Maybe if I
only want to get children that are SVGRect element ?


(3) To know which node represents what SVG element. I use the
inherited org.w3c.dom.Node.getNodeName(), and compare with strings
such as "path", "polyline", etc. Again, is the correct way ? Is there
a SVG DOM api that can achive the same effect without comparing each
strings ? Maybe a .isPath() method, for example.


** Obviously I am new to XML, SVG and Batik. Thanks you in advance for
answering these basic questions :) **

Thanks.

~KEGan

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


Re: Navigating SVG DOM

Posted by Thomas DeWeese <Th...@Kodak.com>.
KE Gan wrote:

> (2) Use the inherited org.w3c.dom.Element.getElementByTagName() to get
> the list of nodes with the same tag name.

    You probably need to use the 'getElementByTagNameNS' calls and
use the SVG Namespace to find SVG elements.

> (3) Use the org.w3c.dom.Element.getElementByTagName(), then use cast
> the node to the appropriate class. Eg,
> 
> NodeList list = root.getElementsByTagName("rect");

    Same comment on namespace stuff...

> for(int i = 0; i < list.getLength(); i++) {
> 
> SVGRectElement rect = (SVGRectElement)list.item(i);

     Yup!

> Still ... must I type the string "rect" at --
> root.getElementsByTagName("rect"); -- Is there a string constant I can
> refer somewhere ?

   Batik has a bunch of constants classes you can use:
	org.apache.batik.util.SVGConstants
			      XMLConstants
                               CSSConstants

   The SVGConstants class has constants for all of the SVG elements
(as well as the SVG Namespace).

> (4) By the way, what is the different between SVGRect and SVGRectElement ?

    SVGRect is used for things like bounding boxes and has no
element/node that participates in the DOM tree.  The SVGRectElement
is the DOM representation of an SVG 'rect' element.

>>(1) The sample in Batik's website show that to get a SVGDocument
>>instance, I have to cast from a org.w3c.dom.Document, which in turn I
>>obtain from SAXSVGDocumentFactory.
>>
>>String parser = XMLResourceDescriptor.getXMLParserClassName();
>>SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
>>InputStream is = new FileInputStream("svgdom\\resource\\sample.svg");
>>Document doc = f.createDocument(null, is);
>>SVGDocument svgDoc = (SVGDocument)doc;
>>
>>Is this the most appropriate way ? Can I load a SVG file (into
>>SVGDocument) without using org.w3c.dom.Document as the medium ?

    Well it's not like the cast 'changes' the doc it's just
going from a baseclass to a subclass.  As Tonny mentioned there
are interfaces on the SAXSVGDocumentFactory that return an
SVGDocument.  The DOM is quite font of returning baseclasses
and requiring downcasting.


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


Re: Navigating SVG DOM

Posted by KE Gan <kh...@gmail.com>.
I think I am going to answer some of my own questions B-) Just happen
to understand more today.

(2) Use the inherited org.w3c.dom.Element.getElementByTagName() to get
the list of nodes with the same tag name.


(3) Use the org.w3c.dom.Element.getElementByTagName(), then use cast
the node to the appropriate class. Eg,

NodeList list = root.getElementsByTagName("rect");

for(int i = 0; i < list.getLength(); i++) {

SVGRectElement rect = (SVGRectElement)list.item(i);
// do something useful with - rect -

}

Still ... must I type the string "rect" at --
root.getElementsByTagName("rect"); -- Is there a string constant I can
refer somewhere ?


(4) By the way, what is the different between SVGRect and SVGRectElement ?


** Please comment on question (1), and the rest as well if required ?

Thanks.

~KEGan



On Tue, 8 Mar 2005 01:09:11 +0800, KE Gan <kh...@gmail.com> wrote:
> Hi,
> 
> I want to manipulate a SVG document (loaded from a file) using Batik's
> SVG DOM api (org.apache.batik.dom.svg).
> 
> (1) The sample in Batik's website show that to get a SVGDocument
> instance, I have to cast from a org.w3c.dom.Document, which in turn I
> obtain from SAXSVGDocumentFactory.
> 
> String parser = XMLResourceDescriptor.getXMLParserClassName();
> SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
> InputStream is = new FileInputStream("svgdom\\resource\\sample.svg");
> Document doc = f.createDocument(null, is);
> SVGDocument svgDoc = (SVGDocument)doc;
> 
> Is this the most appropriate way ? Can I load a SVG file (into
> SVGDocument) without using org.w3c.dom.Document as the medium ?
> 
> (2) To navigate the SVG DOM, I started by calling
> SVGDocument.getRootElement(), which return a SVGSVGElement. To get the
> list of children nodes from this root, I use the inherited
> org.w3c.dom.Node.getChildNodes().
> 
> NodeList nodeList = svgDoc.getRootElement().getChildNodes();
> 
> Is this the right way go get a list of children nodes ? Is there a SVG
> DOM specific API for getting the list of children nodes ? Maybe if I
> only want to get children that are SVGRect element ?
> 
> (3) To know which node represents what SVG element. I use the
> inherited org.w3c.dom.Node.getNodeName(), and compare with strings
> such as "path", "polyline", etc. Again, is the correct way ? Is there
> a SVG DOM api that can achive the same effect without comparing each
> strings ? Maybe a .isPath() method, for example.
> 
> ** Obviously I am new to XML, SVG and Batik. Thanks you in advance for
> answering these basic questions :) **
> 
> Thanks.
> 
> ~KEGan
>

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


Re: Navigating SVG DOM

Posted by Tonny Kohar <to...@kiyut.com>.
Hi, 
> I want to manipulate a SVG document (loaded from a file) using Batik's
> SVG DOM api (org.apache.batik.dom.svg).
> 
> 
> (1) The sample in Batik's website show that to get a SVGDocument
> instance, I have to cast from a org.w3c.dom.Document, which in turn I
> obtain from SAXSVGDocumentFactory.
> 
> String parser = XMLResourceDescriptor.getXMLParserClassName();
> SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
> InputStream is = new FileInputStream("svgdom\\resource\\sample.svg");
> Document doc = f.createDocument(null, is);
> SVGDocument svgDoc = (SVGDocument)doc;
> 
> Is this the most appropriate way ? Can I load a SVG file (into
> SVGDocument) without using org.w3c.dom.Document as the medium ?

I think this is the correct way, or you can use
f.createSVGDocument(param....) which will return SVGDocument (no need to
cast). The factory createDocument method is exist because it inherit
from its parent which is just generic XML Document Factory.

> (2) To navigate the SVG DOM, I started by calling
> SVGDocument.getRootElement(), which return a SVGSVGElement. To get the
> list of children nodes from this root, I use the inherited
> org.w3c.dom.Node.getChildNodes().
> 
> NodeList nodeList = svgDoc.getRootElement().getChildNodes();
> 
> Is this the right way go get a list of children nodes ? Is there a SVG
> DOM specific API for getting the list of children nodes ? Maybe if I
> only want to get children that are SVGRect element ?

There are many way to navigate SVG DOM:
- use Standard DOM API (getChildNode, getElementById, etc ...)
- use DOM Traversal
- use XPATH ??? does batik support this?

> 
> (3) To know which node represents what SVG element. I use the
> inherited org.w3c.dom.Node.getNodeName(), and compare with strings
> such as "path", "polyline", etc. Again, is the correct way ? Is there
> a SVG DOM api that can achive the same effect without comparing each
> strings ? Maybe a .isPath() method, for example.

You also can use java instanceof eg:
if (node instaceof SVGRectElement) etc

Regards
Tonny Kohar
-- 
Sketsa 
SVG Graphics Editor
http://www.kiyut.com


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