Hola a todos,
Modifiqué el código de esta manera, pero al mapear operativamente estaba obteniendo un error de versión. ¿Estaba usando la versión Jdk 6.1 y esa fue la razón de este 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("Número de filas =" + 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("Nombre : " + (name.item(0)).getNodeValue());
xml.setName((name.item(0)).getNodeValue());
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());
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("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");