Servlet: Problème découpage de xml en noeuds

AC83 -  
 AC83 -
Bonjour, voila j'ai un problème:

je dois créer une servlet qui prend en paramètre un fichier xml, une feuille de style xslt, un fichier pdf, et qui transforme le xml en pdf avec Fop en utilisant la feuille de style xslt. Tout ceci marche correctement, mais je veux maintenant passer un noeud en paramètre pour faire un découpage du xml en fonction de ce noeud, et récupérer la liste des noeuds du xml, pour créer un pdf pour chaque noeud.


Voici le code de ma servlet:

protected void xml2pdf (HttpServletResponse response, File xml,File xsl,File pdf, String node)
    throws ServletException, IOException {
    	
    	if(node != null) {    		
    		try {
                // configure foUserAgent as desired
                FOUserAgent foUserAgent = fopFactory.newFOUserAgent();                       
                fopFactory.setStrictValidation(false);
                              
                // Recherche des noeuds Appartement dans le fichier xml et calcul du nombre de noeuds
                IcsPath ip = new IcsPath(xml.getAbsolutePath(), node);                        
                NodeList nodeSet = ip.getNodes();  
                int n = nodeSet.getLength();       
                
                for(int i = 1; i <= n; i++){                                          	
                	// Création des fichiers
                	File pdffile = new File(getServletConfig().getServletContext().getRealPath(".")+"/resources/pdf/fic" + i + ".pdf");            	
                	
                    // Setup output
    	            OutputStream out = new java.io.FileOutputStream(pdffile);
    	            out = new java.io.BufferedOutputStream(out);
           
    	            try {
    	            	
    	                // Construct fop with desired output format
    	                Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);	                
    	            	            	
    	                StringReader sr= new StringReader(node2String(nodeSet.item(i-1)));
                    
    	                // Setup input for XSLT transformation
    	                Source src = new StreamSource(sr);                                   	        
                          	
//    	              Setup XSLT               
    	                Transformer transformer = tFactory.newTransformer(new StreamSource(xsl));
    	                
    	                // Set the value of a <param> in the stylesheet
    	                transformer.setParameter("versionParam", "2.0");
    	                
    		         //Set the value of a <param> in the stylesheet
    	                // Resulting SAX events (the generated FO) must be piped through to FOP
    	                Result res = new SAXResult(fop.getDefaultHandler());	                
    	    
    	                // Start XSLT transformation and FOP processing
    	                transformer.transform(src, res);   
    	                
    	                
    	            } catch(Exception e){
    	            	e.printStackTrace(System.err);
    	            }
                    finally {
                    out.close();
                           
                    }
                                        
                } // end for
                
                
            } // end try
            catch (Exception e) {
                e.printStackTrace(System.err);
            }      
    	}


Voici le code de mon "trieur de noeuds", qui me renvoie une liste vide avec un path pourtant correct:

NodeList nodes;   
public IcsPath(String xmlDocument,String exp) {
        
        nodes = null ;     	
        
        XPathFactory  factory=XPathFactory.newInstance();
        XPath xPath=factory.newXPath();
        try{
                                  
            InputSource inputSource = new InputSource(new FileInputStream(
                    new File(xmlDocument)
                    ));
         
            XPathExpression  xPathExpression= xPath.compile(exp);   
               
            nodes =  (NodeList) xPathExpression.evaluate
                    (inputSource, XPathConstants.NODESET); 
           
        }catch (Exception e){
            e.printStackTrace();
        }
        

    }


Le truc c'est que tout marche comme il faut dans un main, mais pas dans la servlet malheureusement.
Aidez-moi svp.. Merci d'avance!

1 réponse

AC83
 
Bon ben finalement, j'ai trouvé une solution.
Alors, pour ceux que ça pourrait intéresser,

j'ai changé la ligne:
nodes = (NodeList) xPathExpression.evaluate
(inputSource, XPathConstants.NODESET);

par:
nodes = (NodeList) xPath.evaluate
(inputSource, XPathConstants.NODESET);

... et ça marche!
0