Quantcast
Channel: SCN : All Content - Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 7030

Regarding clarifications in my java codings

$
0
0

Hi friends,

 

i have the following input and output structures.i have used java mapping for getting the required output.but my coding is not working properly.

I kindly request people to correct mistakes in my code since i m a fresher to PI.

 

INPUT STRUCTURE:

<?xml version="1.0" encoding="UTF-8" ?> 

-<ns0:MT_javamapping_sender xmlns:ns0="http://cts.com/357356/javamapping">

-<SalesDetails>
-

  <Unit name="chennai">    

-     <SoldCars>

        <Micro>12</Micro>

         <Sedan>12</Sedan>

           <LUV>12</LUV>

</SoldCars>

</Unit>

<Unit name="BANGLORE"> 

-    <SoldCars>

       <Micro>20</Micro>

      <Sedan>20</Sedan>

       <LUV>20</LUV>

</SoldCars>

</Unit>

</SalesDetails>

</ns0:MT_javamapping_sender>

 

output structure:

 

xml version="1.0" encoding="UTF-8"?>

<ns0:MT_javamapping_receiver xmlns:ns0="http://cts.com/357356/javamapping">

<salesoverview>

<unit name="chennai">

        <total>12</total>

</unit>

<unit name="BANGLORE">

        <total>60</total>

</unit>

</salesoverview></ns0:MT_javamapping_receiver>

 

MY JAVA CODINGS

package com;

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;


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.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
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;

 

 

public class Mapping1 extends AbstractTransformation {


public void execute(InputStream in, OutputStream out)throws StreamTransformationException, ParserConfigurationException, SAXException, IOException
{  
  // TODO Auto-generated method stub
try
{
  DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();                 
  DocumentBuilder builderel=factory.newDocumentBuilder(); 
  /*input document in form of XML*/               
  Document docIn=builderel.parse(in);  
     /*document after parsing*/
  Document docOut=builderel.newDocument();  
  TransformerFactory tf=TransformerFactory.newInstance();                
  Transformer transform=tf.newTransformer();
 
  //Document doc;
 
  Element root,unit,sales,salesoverview;
 
  //<?xml version="1.0" encoding="UTF-8" ?>
  // <ns0:MT_javamapping_sender xmlns:ns0="http://cts.com/357356/javamapping">

 
  root=docOut.createElement("ns0:MT_javamapping_receiver");  
  root.setAttribute("xmlns:ns0","http://cts.com/357356/javamapping");
 
  //int micro,sedan,LUV;
 
  Node txtnode;
 
  NodeList units;
  units=(NodeList)docIn.getElementsByTagName("salesdetails");
 
  docOut.appendChild(root);
  salesoverview=docOut.createElement("salesoverview");
 
 
 
  for(int i=0;i<units.getLength();i++)
  {
  
    Node micro =(Node) docIn.getElementsByTagName("micro").item(i);
    Node sedan = (Node) docIn.getElementsByTagName("sedan").item(i);
    Node luv =(Node) docIn.getElementsByTagName("LUV").item(i);
    Node city=docIn.getAttributes().item(i);
    System.out.println(city);
    String place=city.getTextContent();
   
     Integer totals = Integer.parseInt(micro.getFirstChild().getNodeValue())+
                Integer.parseInt(sedan.getFirstChild().getNodeValue())+
                Integer.parseInt(luv.getFirstChild().getNodeValue());
   
     String tot=totals.toString();
    
    
     unit=docOut.createElement("unit");
     unit.setAttribute("name",place);
     //unit.setAttributeNode((Attr) docIn.getAttributes().item(i));
    
     sales=docOut.createElement("total");
     txtnode=docOut.createTextNode(tot);
     sales.appendChild(txtnode);
    
     unit.appendChild(sales);
     salesoverview.appendChild(unit);
    
     //root.appendChild(unit);
    
   }
  root.appendChild(salesoverview);
  transform.transform(new DOMSource(docOut), new StreamResult(out));
  } catch (TransformerException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }  
}


public void transform(TransformationInput arg0, TransformationOutput arg1)throws StreamTransformationException
{  
   // TODO Auto-generated method stub  
try {
  this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
} catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
}  
}  



Viewing all articles
Browse latest Browse all 7030

Trending Articles