Hi Experts,
I am new to Java mapping. My requirement is
Input.xml : -
<?xml version="1.0" encoding="UTF-8" ?>
<ns0:DT_OPERATIONSSOURCE xmlns:ns0="urn:java_mapping2">
<OPERATIONSSOURCE>
<NUMBER1>1</NUMBER1>
<NUMBER2>2</NUMBER2>
</OPERATIONSSOURCE>
</ns0:DT_OPERATIONSSOURCE>
Output.xml:-
<?xml version="1.0" encoding="UTF-8" ?>
<ns0:DT_OPERATIONSTARGET xmlns:ns0="urn:java_mapping2">
<OPERATIONSTARGET>
<ADD>2</ADD>
<MUL>2</MUL>
</OPERATIONSTARGET>
</ns0:DT_OPERATIONSTARGET>
I am unable to get the output. I am getting the following error.
org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.insertBefore(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source)
at giridhar_subPackage.DOMParser111.execute(DOMParser111.java:77)
at giridhar_subPackage.DOMParser111.main(DOMParser111.java:101)
Please suggest me how i can resolvoe this error. Please find my Java File below:
package svs_subPackage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.StreamTransformationException;
public class DOMParser111 extends AbstractTransformation {
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
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();
Element root,child,child1,child2;
Node textChild,textChild1;
Node no1,no2,no3,no4;
String no1s="",no2s="",no3s="",no4s="";
int no1i,no2i,no3i,no4i;
root=docOut.createElement("ns0:DT_OPERATIONSTARGET");
root.setAttribute("xmlns:ns0","urn:java_mapping2");
child2=docOut.createElement("ADD");
root.appendChild(child2);
no1=docIn.getElementsByTagName("NUMBER1").item(0);
no2=docIn.getElementsByTagName("NUMBER2").item(0);
no1i=(Integer.parseInt(no1.getTextContent()))+(Integer.parseInt(no2.getTextContent()));
no1s=Integer.toString(no1i);
textChild=docOut.createTextNode(no1s);
child2.appendChild(textChild);
docOut.appendChild(root);
//root.appendChild(child1);
root.appendChild(child2);
child1=docOut.createElement("MUL");
root.appendChild(child1);
no3=docIn.getElementsByTagName("NUMBER1").item(0);
no4=docIn.getElementsByTagName("NUMBER2").item(0);
no2i=(Integer.parseInt(no3.getTextContent()))*(Integer.parseInt(no4.getTextContent()));
no2s=Integer.toString(no2i);
textChild1=docOut.createTextNode(no2s);
child1.appendChild(textChild1);
docOut.appendChild(root);
root.appendChild(child1);
transform.transform(new DOMSource(docOut), new StreamResult(out));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
try{
DOMParser111 genFormat=new DOMParser111();
FileInputStream in=new FileInputStream("C:\\Users\\pc\\Desktop\\SrcFolder\\input111.xml");
FileOutputStream out=new FileOutputStream("C:\\Users\\pc\\Desktop\\RcvFolder\\output111.xml");
genFormat.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
}
Regards,
GIRIDHAR