How to replace DataXML from Slide Diagram in Powerpoint using Apache POI

139 views Asked by At

i want to replace the one data.xml file of power point presentation in java using apache API with other file data.xml

For the reference i want to replace the following file with another power point file.

enter image description here

Following is the code i have tried but xml isnt replacing. I have different XML for both files every time i run after replacing using this code

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final String filename = "C:/Users/skhan/Desktop/game.pptx";
    final String filename1 = "C:/Users/skhan/Desktop/globe.pptx";

    try {


        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filename));
        OPCPackage pkg = ppt.getPackage();
        PackagePart data = pkg.getPart(
               PackagingURIHelper.createPartName("/ppt/diagrams/data1.xml"));
        InputStream data1Inp = data.getInputStream();

        XMLSlideShow ppt1 = new XMLSlideShow(new FileInputStream(filename1));
        OPCPackage pkg1 = ppt1.getPackage();
        PackagePart data11 = pkg1.getPart(
               PackagingURIHelper.createPartName("/ppt/diagrams/data1.xml"));
        InputStream data1Inp1 = data11.getInputStream();

        String data1String = GetData(data1Inp);
        String data2String = GetData(data1Inp1);



//i want to replace here
        PrintStream pr = new PrintStream(data.getOutputStream());
        pr.print(data2String);
        pr.close();


        System.out.println("Completed");


    } catch (Exception e) {

        e.printStackTrace();
    }

}


public static String GetData(InputStream input) throws Exception
{
    StringBuilder builder = new StringBuilder();
    int ch;
    while((ch = input.read()) != -1){
        builder.append((char)ch);
    }

    String theString = builder.toString();
    return theString;
}
1

There are 1 answers

0
Shan Khan On BEST ANSWER

I added the few line after changing in order to save the file. The XMLSlideShow must write to some file after changing or adding.

        File file =new File(filename);
        FileOutputStream out = new FileOutputStream(file);
        ppt.write(out);
        out.close();