Hello Apache Team,

I recently began to use batik to parse svg images but i have some issues to get 
a specified tag.




        Code  :
        
        1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package batik.test;
 
import java.io.File;
import java.io.IOException;

import javax.xml.xpath.XPathExpressionException;

import org.apache.batik.dom.AbstractDocument;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Node;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.xpath.XPathNSResolver;
import org.w3c.dom.xpath.XPathResult;
 
public class CreateDocFromSVGFile {
 
        public static void main(String[] args) throws XPathExpressionException {
 
                try {
                    String parser = 
XMLResourceDescriptor.getXMLParserClassName();
                    SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
                    File file = new File("C://SVGs_input//figure_text.svg");
                    String uri = file.toURI().toString();
                    SVGDocument svgDocument = f.createSVGDocument(uri);
                    
                    AbstractDocument ab = (AbstractDocument) svgDocument;
                    XPathNSResolver resolver = 
ab.createNSResolver(ab.getDocumentElement());
                    XPathResult result = (XPathResult) ab.evaluate("//text", 
svgDocument.getDocumentElement(), resolver, XPathResult.ANY_TYPE, null);
                    Node node = result.iterateNext();
                    while (node != null) {
                        System.out.println("<" + node.getNodeName() + "> " + 
node.getTextContent());
                        node = result.iterateNext();
                    }
 
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
        }
 
}
The query "//*" return all the tags but as soon as I try to get 
something else like "//title" or "//text", result.iternateNext() returns
 nothing.

I know that the problem comes from the default 
namespace (xmlns="http://www.w3.org/2000/svg";) and I succeeded getting 
what I wanted with jdom with the expression "//svg:text" but it doesn't 
work with batik.





Here is the svg sample :



        Citation:
        
        
                
                        
                                <?xml version="1.0" encoding="utf-8"?>

<svg xmlns="http://www.w3.org/2000/svg"; version="1.1" width="300" height="200">

  <title>Exemple simple de figure SVG</title>

  <desc>Cette figure est constituée d'un rectangle, d'un segment de droite et 
d'un cercle.</desc>

 

  <rect width="100" height="80" x="0" y="70" fill="green" />

  <line x1="5" y1="5" x2="250" y2="95" stroke="red" />

  <circle cx="90" cy="80" r="50" fill="blue" />

   <text x="180" y="60">Un texte</text>

   <text x="180" y="60">Un autre texte</text>

</svg>
                        
                
        
        
And the result with "//*" :




        Citation:
        
        
                
                        
                                svg

title

desc

rect

line

circle

text

text
                        
                
        
        


Thank you in advance                                      

Reply via email to