cancel
Showing results for 
Search instead for 
Did you mean: 

Premature end of file in Java Mapping

Former Member
0 Kudos

Hi All,

I am working on java mapping that which reads XL file and converts to xml.It was working fine when I was testing in Eclipse But while I am testing in Operational mapping it was showing "Unable to display tree view; Error when parsing an XML document (Premature end of file.)";.Kindly do needful.

Please check the below code :

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class Xml_convert extends AbstractTransformation {

          public boolean generateXML(File excelFile) {

                    try {

                              DocumentBuilderFactory factory = DocumentBuilderFactory

                                                  .newInstance();

                              DocumentBuilder builder = factory.newDocumentBuilder();

                              Document document = builder.newDocument();

                              Element catalogElement = document.createElement("document");

                              document.appendChild(catalogElement);

                              InputStream input = new FileInputStream(excelFile);

                              HSSFWorkbook workbook = new HSSFWorkbook(input);

                              HSSFSheet spreadsheet = workbook.getSheetAt(0);

                              System.out

                                                  .println("Number of rows =" + spreadsheet.getLastRowNum());

                              for (int i = 1; i <= spreadsheet.getLastRowNum(); i++) {

                                        HSSFRow row = spreadsheet.getRow(i);

                                        Element journalElement = document.createElement("Element");

                                        catalogElement.appendChild(journalElement);

                                        Element nameElement = document.createElement("name");

                                        journalElement.appendChild(nameElement);

                                        nameElement.appendChild(document.createTextNode(row.getCell(

                                                            (short) 0).toString()));

                                        Element idElement = document.createElement("id");

                                        journalElement.appendChild(idElement);

                                        idElement.appendChild(document.createTextNode(row.getCell(

                                                            (short) 1).toString()));

                                        Element desgElement = document.createElement("desg");

                                        journalElement.appendChild(desgElement);

                                        desgElement.appendChild(document.createTextNode(row.getCell(

                                                            (short) 2).toString()));

                              }

                              TransformerFactory tFactory = TransformerFactory.newInstance();

                              Transformer transformer = tFactory.newTransformer();

                              DOMSource source = new DOMSource(document);

                              StreamResult result = new StreamResult(new File("C:\\file.xml"));

                              transformer.transform(source, result);

                              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                              DocumentBuilder db = dbf.newDocumentBuilder();

                              Document doc = db.parse("");

                    } catch (Exception e) {

                              e.printStackTrace();

                    }

                    return false;

          }

          public void gettingText(Document doc) {

                    XMl xml = new XMl();

                    Element root = doc.getDocumentElement();

                    NodeList nodeList = doc.getElementsByTagName("Element");

                    for (int i = 0; i < nodeList.getLength(); i++) {

                              Node node = nodeList.item(i);

                              if (node.getNodeType() == Node.ELEMENT_NODE) {

                                        Element element = (Element) node;

                                        NodeList nodelist = element.getElementsByTagName("name");

                                        Element element1 = (Element) nodelist.item(0);

                                        NodeList name = element1.getChildNodes();

                                        System.out.println("Name : " + (name.item(0)).getNodeValue());

                                        xml.setName((name.item(0)).getNodeValue()); // For retriving

                                                                                                                                                      // text from node

                                                                                                                                                      // City

                                        Element element2 = (Element) node;

                                        NodeList nodelist1 = element2.getElementsByTagName("id");

                                        Element element3 = (Element) nodelist1.item(0);

                                        NodeList id = element3.getChildNodes();

                                        System.out.println("id : " + (id.item(0)).getNodeValue());

                                        xml.setId((id.item(0)).getNodeValue()); // For retriving text

                                                                                                                                            // from node Phoneno

                                        Element element4 = (Element) node;

                                        NodeList nodelist2 = element4.getElementsByTagName("desg");

                                        Element element5 = (Element) nodelist2.item(0);

                                        NodeList desg = element5.getChildNodes();

                                        System.out.println("desg:" + (desg.item(0)).getNodeValue());

                                        xml.setDesg((desg.item(0)).getNodeValue());

                                        // System.out.println("........." + xMl.getDesg()); //

                                        // System.out.println("........." + xml.getName()); //

                                        // System.out.println("........." + xml.getId());

                                        System.out.println("Name  ="+xml.getName());

                                        System.out.println("Id ="+xml.getId());

                              }

                    }

          }

          public static void main(String[] argv) throws ParserConfigurationException,

                              SAXException, IOException {

                    Xml_convert convert = new Xml_convert();

                    File input = new File("C:/test.xls");

                    convert.generateXML(input);

                    File f = new File("C:\\file.xml");

                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                    DocumentBuilder db = dbf.newDocumentBuilder();

                    Document doc = db.parse(f);

                    convert.gettingText(doc);

          }

          /*

           * public static void main(String[] args) {

           * System.out.println("main ethod"); Xml_convert convert = new (); File

           * excelFile = new File("C:/test.xls"); convert.generateXML(excelFile);

           *

           * }

           */

          @Override

          public void transform(TransformationInput arg0, TransformationOutput arg1)

                              throws StreamTransformationException {

                    // TODO Auto-generated method stub

          }

}

///* another class*//

public class XMl {

          private String id;

          private String name;

          private String desg;

          public String getId() {

                    return id;

          }

          public void setId(String id) {

                    this.id = id;

          }

          public String getName() {

                    return name;

          }

          public void setName(String name) {

                    this.name = name;

          }

          public String getDesg() {

                    return desg;

          }

          public void setDesg(String desg) {

                    this.desg = desg;

          }

}

Accepted Solutions (1)

Accepted Solutions (1)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Premature end of file occurs normally if you dont close or complete the xml tags properly. Please check your code and see if the output generated has no xml syntax error. Also see if you have any namespace issue.

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi All,

I had modified the code like this ,But in Operational mapping I was getting Version Error .I was using Jdk 6.1 version this was  the reason for this error??

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class Xml_convert extends AbstractTransformation {

    public boolean generateXML(InputStream inputStream, OutputStream outputStream) {

        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory

                    .newInstance();

            DocumentBuilder builder = factory.newDocumentBuilder();

            Document document = builder.newDocument();

            Element catalogElement = document.createElement("document");

            document.appendChild(catalogElement);

            InputStream input = inputStream;

            HSSFWorkbook workbook = new HSSFWorkbook(input);

            HSSFSheet spreadsheet = workbook.getSheetAt(0);

            System.out

                    .println("Number of rows =" + spreadsheet.getLastRowNum());

            for (int i = 1; i <= spreadsheet.getLastRowNum(); i++) {

                HSSFRow row = spreadsheet.getRow(i);

                Element journalElement = document.createElement("Element");

                catalogElement.appendChild(journalElement);

                Element nameElement = document.createElement("name");

                journalElement.appendChild(nameElement);

                nameElement.appendChild(document.createTextNode(row.getCell(

                        (short) 0).toString()));

                Element idElement = document.createElement("id");

                journalElement.appendChild(idElement);

                idElement.appendChild(document.createTextNode(row.getCell(

                        (short) 1).toString()));

                Element desgElement = document.createElement("desg");

                journalElement.appendChild(desgElement);

                desgElement.appendChild(document.createTextNode(row.getCell(

                        (short) 2).toString()));

            }

            TransformerFactory tFactory = TransformerFactory.newInstance();

            Transformer transformer = tFactory.newTransformer();

            DOMSource source = new DOMSource(document);

            StreamResult result = new StreamResult(outputStream);

            transformer.transform(source, result);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            DocumentBuilder db = dbf.newDocumentBuilder();

            Document doc = db.parse("");

        } catch (Exception e) {

            e.printStackTrace();

        }

        return false;

    }

    public void gettingText(Document doc) {

        XMl xml = new XMl();

        Element root = doc.getDocumentElement();

        NodeList nodeList = doc.getElementsByTagName("Element");

        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE) {

                Element element = (Element) node;

                NodeList nodelist = element.getElementsByTagName("name");

                Element element1 = (Element) nodelist.item(0);

                NodeList name = element1.getChildNodes();

                System.out.println("Name : " + (name.item(0)).getNodeValue());

                xml.setName((name.item(0)).getNodeValue()); // For retriving

                                                            // text from node

                                                            // City

                Element element2 = (Element) node;

                NodeList nodelist1 = element2.getElementsByTagName("id");

                Element element3 = (Element) nodelist1.item(0);

                NodeList id = element3.getChildNodes();

                System.out.println("id : " + (id.item(0)).getNodeValue());

                xml.setId((id.item(0)).getNodeValue()); // For retriving text

                                                        // from node Phoneno

                Element element4 = (Element) node;

                NodeList nodelist2 = element4.getElementsByTagName("desg");

                Element element5 = (Element) nodelist2.item(0);

                NodeList desg = element5.getChildNodes();

                System.out.println("desg:" + (desg.item(0)).getNodeValue());

                xml.setDesg((desg.item(0)).getNodeValue());

                // System.out.println("........." + xMl.getDesg()); //

                // System.out.println("........." + xml.getName()); //

                // System.out.println("........." + xml.getId());

                System.out.println("Name  =" + xml.getName());

                System.out.println("Id =" + xml.getId());

            }

        }

    }

    public static void main(String[] argv) throws ParserConfigurationException,

            SAXException, IOException {

        Xml_convert convert = new Xml_convert();

        FileInputStream input = new FileInputStream("C:/test.xls");

        FileOutputStream output = new FileOutputStream("C:\\file.xml");

        convert.generateXML(input, output);

        File f = new File("C:\\file.xml");

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(f);

        convert.gettingText(doc);

    }

    /*

     * public static void main(String[] args) {

     * System.out.println("main ethod"); Xml_convert convert = new (); File

     * excelFile = new File("C:/test.xls"); convert.generateXML(excelFile);

     *

     * }

     */

    @Override

    public void transform(TransformationInput arg0, TransformationOutput arg1)

            throws StreamTransformationException {

        // TODO Auto-generated method stub

        this.generateXML(arg0.getInputPayload().getInputStream(), arg1

                .getOutputPayload().getOutputStream());

    }

}

allamudi_loordh
Active Participant
0 Kudos

Hi,

while creating project give java 1.5 then it will work.

Regards,

Loordh

ambrish_mishra
Active Contributor
0 Kudos

Hi Anusha,

One problem with the code is :

You need to work with inputstream and outputstream objects in PI operation mapping and not like above. This coding is for a standalone program executable on eclipse or any other Java editor but not PI.

Hope it helps!

Ambrish

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

You can refer to this blog in testing binaries using operation mapping

http://scn.sap.com/community/pi-and-soa-middleware/blog/2012/07/08/can-we-test-binary-files-in-inter...

Now as for the code,

          public void transform(TransformationInput arg0, TransformationOutput arg1)

                              throws StreamTransformationException {

                    // TODO Auto-generated method stub

          }

Why is this empty? The TransformationInput is where you read your inputs in XI and TransformationOutput is where you write your output. You need to place your logic inside the transform method.

http://wiki.sdn.sap.com/wiki/display/XI/Using+PI+7.1+API+for+Java+mapping

Hope this helps,

Mark

sateesh_adma
Participant
0 Kudos

We have to use custom adapter module to achive this requirement.

Please follow this link for reading excel file and converting it into xml

http://saptechnical.com/Tutorials/XI/Adapter/Index.htm

let me know if you face any issue

anupam_ghosh2
Active Contributor
0 Kudos

Hi Venky,

                Use of adapter module should be avoided as far as possible. The reasons are as follows

1. Deployment needs authorizations.

2. Testing is only possible after deployment of the module.

3. Progammer should be well versed in EJB to for Annual support and maintainance of the project later on. This might increase project cost since project needs people who are well versed with EJB.

Coming to the present scenario , we can achieve XLS  to XML conversion even without custom adapter.

Regards

Anupam

allamudi_loordh
Active Participant
0 Kudos

Hi

Can you please check the target structure in source code view. did you find the converted one.you can check what you are getting output.

Regards,

Loordh