cancel
Showing results for 
Search instead for 
Did you mean: 

PI Dom Parsing Java Mapping retrieving 1 item

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Marco,

The problem is in line below:


result += PI_Mapping_IF.VerifyNull(((Node) modelURL.item(0)).getNodeValue())+";";

Here you are changing the "result" variable inside the second for loop which you shouldn't do because you need the same value of [ident + ";" + buyer + ";";] from outer for loop in each STRING element.

So you would need to do something like this in the inner for loop:

String resultInner = "";

resultInner = result + PI_Mapping_IF.VerifyNull(((Node) modelURL.item(0)).getNodeValue())+";"; + "||";

So here you will be using the resultInner to create the Text Node as below.

  Text srcxml = xml_out.createTextNode(resultInner);

  STRING.appendChild(srcxml);

  item.appendChild(STRING);

  t_input.appendChild(item);

Also the line [root.appendChild(t_input); ] will need to be placed outside the inner most for loop.

Regards,

Shreyansh Shah

Former Member
0 Kudos

Hi Shreyansh Shah,

As you can see on the attached image, the string is now correct but it ain't generating more than 1 item, and this is my biggest problem.

The line 104-root.appendChild(t_input); was removed and placed on the line 109.

Thank you very much!

Former Member
0 Kudos

Ok.. The problem seem to be with following lines being outside the for loop:

Element STRING = xml_out.createElement("STRING");

Element item = xml_out.createElement("ITEM"); 


First Remove or comment out these lines (line no 24 and 26).


Then place them just before the line no 100 as below and try:


Element STRING = xml_out.createElement("STRING");

Element item = xml_out.createElement("ITEM"); 


Text srcxml = xml_out.createTextNode(result);

STRING.appendChild(srcxml);

item.appendChild(STRING); 

t_input.appendChild(item); 


With this, for each element in inner for loop, a new Item and STRING element should be created and added to t_input element.


Regards,

Shreyansh Shah

Answers (1)

Answers (1)

josantonio_roldnluna
Participant
0 Kudos

Hi Marco Antonio,

Could you send me a sample input document for testing your mapping? Witch PI version have you got in installed?

regards,

Jose.

Former Member
0 Kudos

Hi José,

Look at this sample, the PI version is 7.3.

Thank you.