Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Premature end of file in Java Mapping

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;

          }

}

Former Member

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question