Hi Gurus
I'm trying to create a mapping with java, but i'm having a little trouble to append some lines to the table.
I've attached 2 files, the "wrong" which is the way is working today, and the "correct", the way i need it to be.
Current implemented code is:
public void execute(InputStream in, OutputStream out) throws StreamTransformationException { try { // Inicio do java mapping // getTrace().addInfo("JAVA Mapping Iniciado"); //Log para o PI/XI // Declarações referentes ao XML de entrada DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document xml_in = db.parse(in); // Declarações referentes ao XML de saída Document xml_out = db.newDocument(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Remove o standalone xml_in.setXmlStandalone(true); // Declara a estrutura que a RFC irá receber Element root = xml_out.createElement("ns1:ZFSD_VMOI_UPLOAD_CF_PG"); root.setAttribute("xmlns:ns1","urn:sap-com:document:sap:rfc:functions"); xml_out.appendChild(root); Element i_cenario = xml_out.createElement("I_CENARIO"); root.appendChild(i_cenario); Element t_input = xml_out.createElement("T_INPUT"); // root.appendChild(t_input); Element item = xml_out.createElement("ITEM"); // t_input.appendChild(item); Element STRING = xml_out.createElement("STRING"); // item.appendChild(STRING); Element t_return = xml_out.createElement("T_RETURN"); root.appendChild(t_return); Element item_r = xml_out.createElement("ITEM"); t_return.appendChild(item_r); Element message = xml_out.createElement("MESSAGE"); item_r.appendChild(message); // Verifica se existe algum filho no nó NodeList nodos = xml_in.getChildNodes(); if (nodos.item(0) != null) { // getTrace().addInfo("O nó(XML) possui filhos"); //Log para o PI/XI // Declaração de variáveis String ident = ""; // <Ident> String buyer = ""; // <BuyerLineItemNum> String result = ""; // <Ident>;<BuyerLineItemNum>;<Date>;<QuantityValue> // Inicia a extração das informações do XML try{ // Recupera o nó ShipToParty NodeList nodeShip = xml_in.getElementsByTagName("ns0:ShipToParty"); Node node = nodeShip.item(0); Element elemXML = (Element) node; try{ NodeList nodeIdent = elemXML.getElementsByTagName("ns0:Ident"); Element nameElement = (Element) nodeIdent.item(0); nodeIdent = nameElement.getChildNodes(); // Recupera o valor da chave <Ident> ident = PI_Mapping_IF.VerifyNull(((Node) nodeIdent.item(0)).getNodeValue()); }catch(Exception e){ result += "0.0;"; } // Recupera o nó ListOfMaterialGroupedPlanningDetail NodeList nodeBuyer = xml_in.getElementsByTagName("ns0:MaterialGroupedPlanningDetail"); for (int i = 0; i < nodeBuyer.getLength(); i++) { node = nodeBuyer.item(i); elemXML = (Element) node; try{ // Preenche a chave BuyerLineItemNum NodeList nodeBuyerLine = elemXML.getElementsByTagName("ns0:BuyerLineItemNum"); Element elemBuyerLine = (Element) nodeBuyerLine.item(0); nodeBuyerLine = elemBuyerLine.getChildNodes(); buyer = PI_Mapping_IF.VerifyNull(((Node) nodeBuyerLine.item(0)).getNodeValue()); }catch(Exception e){ buyer += "0;"; } result = ident+";"+buyer+";"; Node nodeDt_Qnt = nodeBuyer.item(i); Element elemDt_Qnt = (Element)nodeDt_Qnt; NodeList nodeValores = elemDt_Qnt.getElementsByTagName("ns0:ScheduleDetail"); for (int j = 0; j < nodeValores.getLength(); j++) { node = nodeValores.item(j); elemXML = (Element) node; try{ // Preenche a chave Date NodeList modelExtra = elemXML.getElementsByTagName("ns0:Date"); Element extraElement = (Element) modelExtra.item(0); modelExtra = extraElement.getChildNodes(); result += PI_Mapping_IF.VerifyNull(((Node) modelExtra.item(0)).getNodeValue())+";"; }catch(Exception e){ result += "//;"; } try { // Preenche a chave QuantityValue NodeList modelURL = elemXML.getElementsByTagName("ns0:QuantityValue"); Element urlElement = (Element) modelURL.item(0); modelURL = urlElement.getChildNodes(); result += PI_Mapping_IF.VerifyNull(((Node) modelURL.item(0)).getNodeValue())+";"; } catch (Exception e) { result += "0.0;"; } // Marca o final do registro result += "||"; // Preenche os nós itens Text srcxml = xml_out.createTextNode(result); STRING.appendChild(srcxml); item.appendChild(STRING); t_input.appendChild(item); root.appendChild(t_input); result = ""; } result = ""; buyer = ""; } }catch(Exception e){ } // Remove o standalone xml_out.setXmlStandalone(true); // Preenche o Cenario Text cenario = xml_out.createTextNode("P&G"); i_cenario.appendChild(cenario); // Preenche mensagem de retorno Text msgxml = xml_out.createTextNode("XML lido com sucesso!"); message.appendChild(msgxml); // Escreve a saida do XML transformer.transform(new DOMSource(xml_out), new StreamResult(out)); } } catch (ParserConfigurationException e) { throw new StreamTransformationException("Can not create DocumentBuilder.", e); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
Thank you all