Results 1 to 10 of 10
  1. #1
    trickyhonesty is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default xslt transformation with java

    hello forum;

    i upload xml files in my hsp page, then in the post method of my servlet i try to make a transformation on those files and display the data on a html page.

    here is the source of my post methode. Can you help me and tell me what's wrong with this source and how i can display the data contains on those xml files?

    Java Code:
     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            
            
            try {
                String xmlFile = request.getParameter("fichier");
                Source xmlInput = new StreamSource(new File(xmlFile));
                Source xsl = new StreamSource(new File("fichxsl.xsl"));
                Result xmlOutput = new StreamResult(new File("visualisation.jsp"));
                Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl);
                transformer.transform(xmlInput, xmlOutput);
            } catch (TransformerException e) {
                // Handle.
            }
        }
    my code doesn't work, what's wrong with it ?
    if there's another way to make display the uploaded xml file's data on a html or jsp page , plzzz tell me how ?
    thnx in advance for your precious help
    Last edited by trickyhonesty; 10-30-2014 at 12:38 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: xslt transformation with java

    First, please wrap your code in [code] tags [/code] so it retains its formatting.

    Second, you haven't actually said what the problem is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: xslt transformation with java

    Well for one the code seems to take a filename with complete path from the request parameters ("fichier") and then try to open that file on the local harddrive where the server is running. That's not what I would expect to happen when you talk about a "file upload".

    Secondly, sending the output to a local file "visualization.jsp" is probably also not going to do what you want it to do.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: xslt transformation with java

    Thank you for the code tags, but you still haven't said what exactly is wrong.
    And no, "it doesn't work" does not count.

    I do note you have nothing at all in your catch block.
    Maybe adding a printStackTrace() in there might help?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: xslt transformation with java

    Try to debug step by step.
    1.check whether ur file got uploaded.
    2.put some logstatements in catch as suggested.
    Ramya:cool:

  6. #6
    trickyhonesty is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default Re: xslt transformation with java

    after making some research, I completely changed my servlet code, I parse the xml document first then make the transformation, but it still does not work, when I choose an xml file, and validate I still have the same message error "Error 404 - Not Found".

    this is the new java code :
    Java Code:
    package edi;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.*;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.w3c.dom.Document;
    import org.w3c.dom.DOMException;
    
    // For write operation
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.transform.stream.StreamResult;
    import java.io.*;
    
    
    @WebServlet(name = "Validation", urlPatterns = { "/validation" })
    public class Validation extends HttpServlet {
        private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
        static Document document;
    
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                   try {
                       String xmlfile=request.getParameter("fichier");
                       String xsltfile="C:\\Documents and Settings\\Administrateur\\Bureau\\test\\EDI3\\EDI";
                       File stylesheet = new File(xmlfile);
                       File datafile = new File(xsltfile);
    
                       DocumentBuilder builder = factory.newDocumentBuilder();
                       document = builder.parse(datafile);
    
                       // Use a Transformer for output
                       TransformerFactory tFactory = TransformerFactory.newInstance();
                       StreamSource stylesource = new StreamSource(stylesheet);
                       Transformer transformer = tFactory.newTransformer(stylesource);
    
                       DOMSource source = new DOMSource(document);
                       StreamResult result = new StreamResult(System.out);
                       transformer.transform(source, result);
                   } catch (TransformerConfigurationException tce) {
                       // Error generated by the parser
                       System.out.println("\n** Transformer Factory error");
                       System.out.println("   " + tce.getMessage());
    
                       // Use the contained exception, if any
                       Throwable x = tce;
    
                       if (tce.getException() != null) {
                           x = tce.getException();
                       }
    
                       x.printStackTrace();
                   } catch (TransformerException te) {
                       // Error generated by the parser
                       System.out.println("\n** Transformation error");
                       System.out.println("   " + te.getMessage());
    
                       // Use the contained exception, if any
                       Throwable x = te;
    
                       if (te.getException() != null) {
                           x = te.getException();
                       }
    
                       x.printStackTrace();
                   } catch (SAXException sxe) {
                       // Error generated by this application
                       // (or a parser-initialization error)
                       Exception x = sxe;
    
                       if (sxe.getException() != null) {
                           x = sxe.getException();
                       }
    
                       x.printStackTrace();
                   } catch (ParserConfigurationException pce) {
                       // Parser with specified options can't be built
                       pce.printStackTrace();
                   } catch (IOException ioe) {
                       // I/O error
                       ioe.printStackTrace();
                   }
        }
    }
    plzzz help me find what's wrong , and how i can make it works, this is realy urgent
    or where i can find a complet tutorial for transformation xml/html with java

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: xslt transformation with java

    If you don't share how you are actually submitting to this servlet, it is impossible to provide any hint as to what is wrong.

    Where are you seeing that 404 error? In your browser?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: xslt transformation with java

    And can I recommend you get a proper logging framework for your app, so you can log stuff instead of SysOuting it.
    You need to check whether you are even hitting the server...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    trickyhonesty is offline Member
    Join Date
    Oct 2014
    Posts
    5
    Rep Power
    0

    Default Re: xslt transformation with java

    hello forum;

    thanx for all ur replies, it was a mistake in my web.xml file, i also modified my code i was switching the xml file and stylesheet file:

    here is my new code :
    Java Code:
     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                   try {
                       String xmlfile=request.getParameter("fichier");
                       String xsltfile="C:\\Documents and Settings\\Administrateur\\Bureau\\test\\EDI3\\EDI\\fichxsl.xsl";
                       File stylesheet = new File(xsltfile);
                       File datafile = new File(xmlfile);
     
                       DocumentBuilder builder = factory.newDocumentBuilder();
                       document = builder.parse(datafile);
     
                       // Use a Transformer for output
                       TransformerFactory tFactory = TransformerFactory.newInstance();
                       StreamSource stylesource = new StreamSource(stylesheet);
                       Transformer transformer = tFactory.newTransformer(stylesource);
     
                       DOMSource source = new DOMSource(document);
                       StreamResult result = new StreamResult(System.out);
                       transformer.transform(source, result);
                   } catch (TransformerConfigurationException tce) {
                       System.err.println("\n** Transformer Factory error");
                                          tce.printStackTrace();
                   } catch (TransformerException te) {
                       System.err.println("\n** Transformer Factory error");
                                          te.printStackTrace();
                   } catch (SAXException sxe) {
                       System.err.println("\n** Transformer Factory error");
                                          sxe.printStackTrace();
                   } catch (ParserConfigurationException pce) {
                       // Parser with specified options can't be built
                       pce.printStackTrace();
                   } catch (IOException ioe) {
                       // I/O error
                       ioe.printStackTrace();
                   }
        }
    }
    and here is the new error message i'm getting while runing my app

    Error 500--Internal Server Error

    java.lang.NullPointerException
    at java.io.File.(File.java:251)
    at edi.Validation.doPost(Validation.java:43)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:751)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:844)
    at weblogic.servlet.internal.StubSecurityHelper$Servl etServiceAction.run(StubSecurityHelper.java:280)
    at weblogic.servlet.internal.StubSecurityHelper$Servl etServiceAction.run(StubSecurityHelper.java:254)
    at weblogic.servlet.internal.StubSecurityHelper.invok eServlet(StubSecurityHelper.java:136)
    at weblogic.servlet.internal.ServletStubImpl.execute( ServletStubImpl.java:341)
    at weblogic.servlet.internal.TailFilter.doFilter(Tail Filter.java:25)
    at weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:79)
    at oracle.security.jps.ee.http.JpsAbsFilter$1.run(Jps AbsFilter.java:137)
    at java.security.AccessController.doPrivileged(Native Method)
    at oracle.security.jps.util.JpsSubject.doAsPrivileged (JpsSubject.java:315)
    at oracle.security.jps.ee.util.JpsPlatformUtil.runJaa sMode(JpsPlatformUtil.java:460)
    at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMo de(JpsAbsFilter.java:120)
    at oracle.security.jps.ee.http.JpsAbsFilter.doFilter( JpsAbsFilter.java:217)
    at oracle.security.jps.ee.http.JpsFilter.doFilter(Jps Filter.java:81)
    at weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:79)
    at oracle.dms.servlet.DMSServletFilter.doFilter(DMSSe rvletFilter.java:225)
    at weblogic.servlet.internal.FilterChainImpl.doFilter (FilterChainImpl.java:79)
    at weblogic.servlet.internal.WebAppServletContext$Ser vletInvocationAction.wrapRun(WebAppServletContext. java:3367)
    at weblogic.servlet.internal.WebAppServletContext$Ser vletInvocationAction.run(WebAppServletContext.java :3333)
    at weblogic.security.acl.internal.AuthenticatedSubjec t.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(Se curityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(Wls SubjectHandle.java:57)
    at weblogic.servlet.internal.WebAppServletContext.doS ecuredExecute(WebAppServletContext.java:2220)
    at weblogic.servlet.internal.WebAppServletContext.sec uredExecute(WebAppServletContext.java:2146)
    at weblogic.servlet.internal.WebAppServletContext.exe cute(WebAppServletContext.java:2124)
    at weblogic.servlet.internal.ServletRequestImpl.run(S ervletRequestImpl.java:1564)
    at weblogic.servlet.provider.ContainerSupportProvider Impl$WlsRequestExecutor.run(ContainerSupportProvid erImpl.java:254)
    at weblogic.work.ExecuteThread.execute(ExecuteThread. java:295)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java :254)

    thanks in advance for your precious help

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: xslt transformation with java

    Pretty sure that this:

    Java Code:
    String xmlfile=request.getParameter("fichier");
    results in a null-value. And then this:

    Java Code:
    File datafile = new File(xmlfile);
    is going to result in the exception you are getting.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Java GUI app used to apply XSLT to XML's
    By fawkes711 in forum XML
    Replies: 1
    Last Post: 12-09-2009, 08:59 AM
  2. Transformation
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 11-19-2008, 04:11 PM
  3. Replies: 0
    Last Post: 10-31-2008, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •