0

I have to trim the lines from the input file and generate the output in SAP PI. This can't be done using graphical mapping in SAP PI, so I use Java and build the code in Eclipse.

I had used BufferedReader to build the logic and it was working fine.. But as the XML file comes into SAP PI, I changed BufferedReader to TransformerFactory and I'm stuck here.

public class TestIntegrate {
    private Document doc = null;
    
    public static void main(String[] args) {
        FileInputStream fin;
        
        try {
            fin = new FileInputStream("C:/Users/xyz/workspace/TEST_2.xml");
            FileOutputStream fout = new FileOutputStream("D:\\ProgramFiles\\eclipse\\mapping\\poslogoutput.xml");
            TestIntegrate t = new TestIntegrate();
            t.execute(fin, fout);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void execute(InputStream sourceFile, OutputStream targetFile) /*throws StreamTransformationException*/ {
        try {
            // Creating the parser object.
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(sourceFile);
            
            writeOutputfile(doc, targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } // End of execute 
    // Write to the output file 
    private void writeOutputfile(Document doc, OutputStream targetFile) {
        try {
            String lineToRemove = "<Tax>";
            String lineToRemove1 = "</Tax>";
            String currentLine;
            TransformerFactory transformFactory = TransformerFactory.newInstance();
            DOMSource source = new DOMSource(doc);
            Transformer transformer = transformFactory.newTransformer();
            Writer outWriter = new StringWriter();
            StreamResult result = new StreamResult(targetFile);
            transformer.transform(source, result);
            
            // Logic to trim the lines
            while ((currentLine = reader.readLine()) != null) /* ERROR: no reader object */ {
                String trimmedLine = currentLine.trim();
                if (trimmedLine.equals(lineToRemove) || trimmedLine.equals(lineToRemove1)) continue;
                outWriter.write(currentLine + System.getProperty("line.separator"));
            }
            
            System.out.println("File saved!");
            reader.close(); //ERROR: no reader object
            writer.close(); //ERROR:no Writer object
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Input file:

<?xml version="1.0" encoding="UTF-8"?>
<School>
    <SSLC>
        <name />
        <rollno />
    </SSLC>
    <PUC>
        <first_pu>
            <name />
            <rollno />
        </first_pu>
        <second_pu>
            <name />
            <rollno />
        </second_pu>
    </PUC>
    <PUC>
        <first_pu>
            <name />
            <rollno />
        </first_pu>
        <second_pu>
            <name />
            <rollno />
        </second_pu>
    </PUC>
    <PUC>
        <first_pu>
            <name />
            <rollno />
        </first_pu>
        <second_pu>
            <name />
            <rollno />
        </second_pu>
    </PUC>
</School>

Thanks in advance.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
user3497375
  • 45
  • 2
  • 2
  • 6

0 Answers0