Hi Experts,
I am new to DOM Parsing Java Mapping.For learning purpose I want to do one DOM Parsing,I am working in SAP PI Version 7.3.
My requirement is very simple and I am illustrating that below:
Source Structure is :
DT_Source
>Person1
>Employee
>Name
>Surname
and Target Structure that needs to be generated is as below:
DT_Target
>Person2
>EmpName
>Emp
Emp will be generated by concatenating Name and Surname from Source.
I have written one code which is given below:
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.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
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 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 MergeName extends AbstractTransformation{
public void transform1(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder = null;
try {
dbuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
InputStream in = null;
doc = dbuilder.parse(in);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
doc.getFirstChild();
doc.getChildNodes();
DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder1 = null;
try {
dbuilder1 = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document docout=dbuilder1.newDocument();
Node Name;
Node Surname,textChild;
String fullname="";
Element Person2=docout.createElementNS("http://testing", "Person2");
//docout.appendChild(Person2);
Element EmpName=docout.createElement("EmpName");
//Person2.appendChild(EmpName);
Element Emp=docout.createElement("Emp");
EmpName.appendChild(Emp);
Name=(Node) doc.getElementsByTagName("Name").item(0);
Surname=(Node) doc.getElementsByTagName("Surname").item(0);
fullname=Name.getFirstChild().getNodeValue();
fullname+=Surname.getFirstChild().getNodeValue();
textChild=docout.createTextNode(fullname);
Emp.appendChild(textChild);
docout.appendChild(Person2);
Person2.appendChild(EmpName);
Transformer tFormer = null;
try {
tFormer = TransformerFactory.newInstance().newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}
Source source = new DOMSource(docout);
OutputStream out = null;
StreamResult result = new StreamResult(out);
try {
tFormer.transform(source, result);
} catch (TransformerException e) {
e.printStackTrace();
}
}
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
}
//public void setParameter(Map arg0) {
//}
}
But everytime I am executing this I am getting an error that is
" Unable to display tree view; Error when parsing an XML document (Premature end of file.)"
I check the code but not getting where the error is actually.
Please help me in getting the correct code for implementing this.
Thanks and Regards
Joy