0

I'm creating an integration solution with Java that filters and modify huge XML files. Those XML files are inputted as a payload document through the solution and to do a big filter in the parts that are interesting for me I want to use XSLT stylesheet. My difficult is that the default Java solution for this does not work for me (XSLT processing with Java?) as I do not want to take the XML from the system, once it is already in the workflow of the solution, and I need the output source to stay in the workflow.

Element production = docX2.createElement("PRODUCTION");
              try {
                  TransformerFactory factory = TransformerFactory.newInstance();
                  Source xslt = new StreamSource("slimmer.xslt");
                  Transformer transformer = factory.newTransformer(xslt);
                  Source text = new StreamSource((InputStream) docX1);
                  transformer.transform(text, new StreamResult((OutputStream) production));
                } catch (Exception ex) {
                  Logger.getLogger(IntProcess.class.getName()).log(Level.SEVERE, null, ex);
                }
 root.appendChild(production);

docX1 is the XML input document that is flowing through the solution, and docX2 is the output document (both are Document class in Java). Production is a tag element from docX2.

  • Are the used `Document` and `Element` classes or interfaces the W3C DOM ones? I don't understand the attempted cast `(OutputStream) production`. If you want to populate a DOM document or element node use a https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/dom/DOMResult.html#DOMResult(org.w3c.dom.Node) from that node. – Martin Honnen Nov 14 '18 at 16:37

1 Answers1

0

I solve it. With the help of this one Transform XML with XSLT in Java using DOM My solution is

Element production = docX2.createElement("PRODUCTION");
                try {
                    TransformerFactory factory = TransformerFactory.newInstance();
                    Source xslt = new StreamSource("slimmer.xslt");
                    Transformer transformer = factory.newTransformer(xslt);
                    Source text = new DOMSource(docX1);                    
                    transformer.transform(text, new DOMResult(production));                    
                } catch (Exception ex) {
                    Logger.getLogger(IntProcess.class.getName()).log(Level.SEVERE, null, ex);
                }
                root.appendChild(production);

The problem was to try using Stream instead of a DOM source.