on 04-28-2016 5:56 AM
Hi Experts,
I am working on SAP PI 7.31 single stack and requirement is xml to xls conversion via Java mapping and I am using eclipse mars workbench due to some issues in NWDS. My JDK version is as per attached snippet:
But after compiling the attached code it is throwing error " LinkageError at JavaMapping.load(): Could not load class: "java.lang.UnsupportedClassVersionError:: Unsupported major.minor version"" 51.0 "
Can you please suggest.
Thanks,
Nithin.
hi Experts,
Still I am facing the same error, " Mapping failed in runtimeLinkage Error when loading class; details: java.lang.NoClassDefFoundError, LinkageException: Linkage Error when loading class TestMap/Xml2Xls; details: java.lang.NoClassDefFoundError, NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook ". Can you please guide.
Thanks,
Naveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Naveen,
What is the procedure you used to export the Jar file? Please follow the below steps to export the Jar file from NWDS and importing it in ESR -
1. Right Click on your project, click export -> java -> jar file -> click browse -> provide file name
2. Go to ESR -> Create Imported Archive -> Import Archive -> choose the above provided file name
3. Create Operation Mapping -> Use the above IA
I guess you exported the java project using General -> Archive File, hence this error.
Regards,
Nitin
Hi Nithin,
I reused the logic used in the module, you can use below java mapping.
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
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 XML2ExcelJavaMap extends AbstractTransformation {
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
ArrayList<ArrayList<String>> contents = new ArrayList<ArrayList<String>>();
SAXSimpleParser handler = new SAXSimpleParser(contents);
parser.parse(transformationInput.getInputPayload().getInputStream(), handler);
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
for (int i = 0; i < contents.size(); i++) {
Row row = sheet.createRow(i);
ArrayList<String> contentRow = contents.get(i);
for (int j = 0; j < contentRow.size(); j++) {
Cell cell = row.createCell(j);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(contentRow.get(j));
}
}
wb.write(transformationOutput.getOutputPayload().getOutputStream());
} catch (Exception e) {
throw new StreamTransformationException(e.getMessage(), e);
}
}
private class SAXSimpleParser extends DefaultHandler {
private int level;
private String fieldName = null;
private String fieldValue = "";
private final ArrayList<ArrayList<String>> contents;
private ArrayList<String> row;
public SAXSimpleParser(ArrayList<ArrayList<String>> contents) {
this.contents = contents;
}
@Override
public void startDocument() throws SAXException {
this.level = 0;
}
@Override
public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException {
if (this.level == 1) {
this.row = new ArrayList<String>();
}
if (this.level == 2) {
this.fieldName = rawName;
}
this.level++;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (this.fieldName != null) {
this.row.add(this.fieldValue);
}
this.fieldName = null;
this.fieldValue = "";
this.level--;
if (this.level == 1) {
this.contents.add(this.row);
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (this.fieldName != null) {
char[] ch2 = new char[length];
System.arraycopy(ch, start, ch2, 0, length);
this.fieldValue = this.fieldValue + new String(ch2);
}
}
}
}
Import poi 3.5 Final jar file as imported archive in repository.
I used the xml which you referred the thread and the Excel file created for that xml:
Regards,
Praveen.
Hi Experts,
Additional information is the JDK version installed in PI Linux server is 1.7 and the java code executed on eclipse with Java version of 1.6.
Thanks,
Naveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Experts,
Kindly help me to resolve the issue. My requirement is xml to xls conversion through java mapping.
Thanks,Naveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Praveen,
I am using the same code as per blog https://scn.sap.com/thread/3224533 i.e., as below or else pls guide me with right code.
package JavaTest;
//import java.io.File;
//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 jxl.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.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 Xml2Xls extends AbstractTransformation
{
public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException
{
try
{
getTrace().addInfo("JAVA Mapping Called");
//Workbook w = Workbook.getWorkbook(arg0.getInputPayload().getInputStream());
HSSFWorkbook wb= new HSSFWorkbook();
HSSFSheet spreadSheet=wb.createSheet("spreadSheet");
spreadSheet.setColumnWidth((short) 0,(short) (256*25));
spreadSheet.setColumnWidth((short) 1,(short) (256*25));
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
//InputStream xmlDocuments = null;
Document document= builder.parse(arg0.getInputPayload().getInputStream());
NodeList nodelist=document.getElementsByTagName("stmt");
HSSFRow row=spreadSheet.createRow(0);
HSSFCell cell=row.createCell((short)1);
cell.setCellValue("Year 2005");
cell=row.createCell((short)2);
cell.setCellValue("Year 2004");
HSSFRow row1 = spreadSheet.createRow(1);
HSSFRow row2 = spreadSheet.createRow(2);
HSSFRow row3 = spreadSheet.createRow(3);
HSSFRow row4 = spreadSheet.createRow(4);
HSSFRow row5 = spreadSheet.createRow(5);
HSSFRow row6 = spreadSheet.createRow(6);
HSSFRow row7 = spreadSheet.createRow(7);
HSSFRow row8 = spreadSheet.createRow(8);
HSSFRow row9 = spreadSheet.createRow(9);
HSSFRow row10 = spreadSheet.createRow(10);
HSSFRow row11 = spreadSheet.createRow(11);
for(int i=0;i<nodelist.getLength(); i++)
{
HSSFCellStyle cellStyle=wb.createCellStyle();
cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
switch(i)
{
case 0:
cell = row1.createCell((short) 0);
cell.setCellValue("Revenue ($)");
cell=row1.createCell((short)1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("revenue").item(0)
.getFirstChild().getNodeValue());
cell = row2.createCell((short) 0);
cell.setCellValue("Cost of Revenue ($)");
cell = row2.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("costofrevenue").item(0)
.getFirstChild().getNodeValue());
cell = row3.createCell((short) 0);
cell.setCellValue("Research and Development ($)");
cell = row3.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("researchdevelopment")
.item(0).getFirstChild().getNodeValue());
cell = row4.createCell((short) 0);
cell.setCellValue("Sales and Marketing ($)");
cell = row4.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("salesmarketing").item(0)
.getFirstChild().getNodeValue());
cell = row5.createCell((short) 0);
cell.setCellValue("General and Administrative ($)");
cell = row5.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("generaladmin").item(0)
.getFirstChild().getNodeValue());
cell = row6.createCell((short) 0);
cell.setCellValue("Total Operating Expenses ($)");
cell.setCellStyle(cellStyle);
cell = row6.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("totaloperexpenses").item(0)
.getFirstChild().getNodeValue());
cell.setCellStyle(cellStyle);
cell = row7.createCell((short) 0);
cell.setCellValue("Operating Income ($)");
cell = row7.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("operincome").item(0)
.getFirstChild().getNodeValue());
cell = row8.createCell((short) 0);
cell.setCellValue("Investment Income ($)");
cell = row8.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("invincome").item(0)
.getFirstChild().getNodeValue());
cell = row9.createCell((short) 0);
cell.setCellValue("Income Before Taxes ($)");
cell.setCellStyle(cellStyle);
cell = row9.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("incbeforetaxes").item(0)
.getFirstChild().getNodeValue());
cell.setCellStyle(cellStyle);
cell = row10.createCell((short) 0);
cell.setCellValue("Taxes ($)");
cell = row10.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("taxes").item(0)
.getFirstChild().getNodeValue());
cell = row11.createCell((short) 0);
cell.setCellValue("Net Income ($)");
cell.setCellStyle(cellStyle);
cell = row11.createCell((short) 1);
cell.setCellValue(((Element) (nodelist.item(0)))
.getElementsByTagName("netincome").item(0)
.getFirstChild().getNodeValue());
cell.setCellStyle(cellStyle);
break;
case 1:
cell = row1.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("revenue").item(0)
.getFirstChild().getNodeValue());
cell = row2.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("costofrevenue").item(0)
.getFirstChild().getNodeValue());
cell = row3.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("researchdevelopment")
.item(0).getFirstChild().getNodeValue());
cell = row4.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("salesmarketing").item(0)
.getFirstChild().getNodeValue());
cell = row5.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("generaladmin").item(0)
.getFirstChild().getNodeValue());
cell = row6.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("totaloperexpenses").item(0)
.getFirstChild().getNodeValue());
cell.setCellStyle(cellStyle);
cell = row7.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("operincome").item(0)
.getFirstChild().getNodeValue());
cell = row8.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("invincome").item(0)
.getFirstChild().getNodeValue());
cell = row9.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("incbeforetaxes").item(0)
.getFirstChild().getNodeValue());
cell.setCellStyle(cellStyle);
cell = row10.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("taxes").item(0)
.getFirstChild().getNodeValue());
cell = row11.createCell((short) 2);
cell.setCellValue(((Element) (nodelist.item(1)))
.getElementsByTagName("netincome").item(0)
.getFirstChild().getNodeValue());
cell.setCellStyle(cellStyle);
break;
default:
break;
}
}
wb.write(arg1.getOutputPayload().getOutputStream());
// FileOutputStream output = new FileOutputStream(new File("IncomeStatements.xls"));
// wb.write(output);
// output.flush();
// output.close();
} catch (IOException e)
{
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Please use the sample xml attached in the blog https://scn.sap.com/thread/3224533
Thanks,
Naveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Bhavesh,Praveen,
Thanks for your reply.
Now I am facing Linkage error during runtime. Mapping failed in runtimeLinkage Error when loading class TestXML/test; details: java.lang.NoClassDefFoundError, LinkageException: Linkage Error when loading class TestXML/test; details: java.lang.NoClassDefFoundError, NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
Please suggest.
Thanks,
Nithin.
Hi Naveen,
You must download the jar (Download jakarta-poi-2.5.jar : jakarta&nbsp;&laquo;&nbsp;j&nbsp;&laquo;&nbsp...) and import it like library in your SAP NWDS.
Regards.
Hi Praveen,
In eclipse no warning/errors are thrown after importing the JARS. Below first snippet shows JARS I included in external library and my requirement is xml to xls conversion for which I am trying out the attached code. Please help.
I am using code for xml to xls conversion following link: https://scn.sap.com/thread/3224533 .
But I am still facing error.
Thanks,
Nithi.
Hi Praveen,
Thank you for your time. I have imported all the jars in to source folder, but still the same error.
" Mapping failed in runtimeLinkage Error when loading class TestMap/Xml2Xls; details: java.lang.NoClassDefFoundError, LinkageException: Linkage Error when loading class details: java.lang.NoClassDefFoundError, NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook "
Thanks,
Naveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.