0

我必须修剪输入文件中的行并在 SAP PI 中生成输出。这不能使用 sap PI 中的图形映射来完成,所以我使用 java 并在 Eclipse 中构建代码。

我曾经BufferedReader构建逻辑并且工作正常。但是随着 XML 文件进入 PI,我将 BufferedReader 更改为TransformerFactory并且我被困在这里。

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();
        }
    }
}

输入文件:

<?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>

提前致谢

4

0 回答 0