on 05-09-2012 9:14 AM
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
Hi Atanu,
How are you? hope you are doing well. Please upload the complete source and target xml and explain mapping logic, if any you need in code. I will try this one out.
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anupam,
I am fine.How are you?Actually previously I had done Java Mapping DOM parsing in PI 7.0.Now when I am trying to execute those codes in PI 7.3 version the same code is not running.Latter I came to know that there are some changes needs to be done for PI 7.3. Like in 7.0 we have used "public class XXXX implements StreamTransformation" but in PI 7.3 it should be "public class XXXX implements AbstractTransformation" then in PI 7.0 we have used "public void execute(InputStream in, OutputStream out)" but in PI 7.3 we should use "public void transform(InputStream in, OutputStream out)". So I thought of running one basic DOM parser code successfully in PI 7.3 first before going to my work requirement.
This requirement is all about concatenating Name and Surname from Source structure and creating Full_Name in target structure.
The Source structure is as given below:
DT_Source
>Person1
>Employee
>Name
>Surname
and Target Structure that needs to be generated is as below:
DT_Target
>Person2
>EmpName
>Emp
where field "Emp" will be generated by concatenating Name and Surname from Source.
I need to run this on PI 7.3 version.If you can provide with the code then it will be of great help to me.
Thanks and Regards
Joy
Hi Atanu,
Sorry for the delay in response as I have been quite busy for last few days. Today i could make out some time to work with java.
Here is input.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ns0:DT_Source xmlns:ns0="urn:java_mapping2">
<Person1>
<Name>Alex</Name>
<Surname>Stewart</Surname>
</Person1>
</ns0:DT_Source>
here is the output.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:DT_Target xmlns:ns0="urn:java_mapping2">
<Person2>
<EmpName>
<Emp>Alex Stewart</Emp>
</EmpName>
</Person2>
</ns0:DT_Target>
the java mapping code is shown below.
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 DOMParser1 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;
Node name,surname;
String fullname="";
root=docOut.createElement("ns0:DT_Target");
root.setAttribute("xmlns:ns0","urn:java_mapping2");
child1=docOut.createElement("Person2");
child=docOut.createElement("EmpName");
child2=docOut.createElement("Emp");
child.appendChild(child2);
child1.appendChild(child);
name=docIn.getElementsByTagName("Name").item(0);
surname=docIn.getElementsByTagName("Surname").item(0);
fullname=name.getFirstChild().getNodeValue()+" ";
fullname+=surname.getFirstChild().getNodeValue();
textChild=docOut.createTextNode(fullname);
child2.appendChild(textChild);
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{
DOMParser1 genFormat=new DOMParser1();
FileInputStream in=new FileInputStream("C:\\apps\\test\\input1.xml");
FileOutputStream out=new FileOutputStream("C:\\apps\\test\\output1.xml");
genFormat.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
}
Hope this resolves your problem.
Regards
Anupam
Hi Joy,
first run your JAVA Mapping program locally using NWDS or eclipse, just add main method to your program and test it.
by seeing your thread i can say the output stream not writing valid XML file as output.
Best Regards,
Raj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
there are plenty of examples avaialble on SDN please do a simple search - http://search.sap.com/ui/scn#query=dom+parser&startindex=1&filter=scm_a_site(scm_v_Site11)&filter=sc...
http://scn.sap.com/people/community.user/blog/2008/03/10/java-dom-de-mystified-starter-kit
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Shabarish,
I have gone through the links provided by you but it doesn't serve my purpose.
I donot want the code but I need where am I doing the mistake for which the it is displaying the error
"Unable to display tree view; Error when parsing an XML document (Premature end of file.)",I mean under what circumstances it usually throws such error?
I am again sending the updated code 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.w3c.dom.NodeList;
import org.w3c.dom.Text;
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 transform(InputStream in, OutputStream out)
throws StreamTransformationException {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder = null;
try {
dbuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
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();
NodeList Employee;
Element Emp = null;Element EmpName = null;Element Person2=null;
Element Target=docout.createElementNS("http://testing", "MT_Target");
Employee=(NodeList) doc.getElementsByTagName("Employee");
for(int j=0;j<=((Employee.getLength())-1);j++)
{
Person2=docout.createElement("Person2");
EmpName=docout.createElement("EmpName");
Emp=docout.createElement("Emp");
EmpName.appendChild(Emp);
Node Name =(Node) doc.getElementsByTagName("Name").item(j);
Node Surname = (Node) doc.getElementsByTagName("Surname").item(j);
String fullname = Name.getFirstChild().getNodeValue();
fullname+=Surname.getFirstChild().getNodeValue();
Text textChild = docout.createTextNode(fullname);
Emp.appendChild(textChild);
Person2.appendChild(EmpName);
Target.appendChild(Person2);
}
docout.appendChild(Person2);
// Node Name1 = Name.item(j);
// fullname=Name1.getFirstChild().getNodeValue();
//Node Surname1 = Surname.item(k);
//fullname+=Surname1.getFirstChild().getNodeValue();
//Emp.appendChild((Node) textChild);
//fullname+=((Node) Surname).getFirstChild().getNodeValue();
//}
//Name=(NodeList) doc.getElementsByTagName("Name");
//Surname=(NodeList) doc.getElementsByTagName("Surname");
//for(int i=0;i<=((Employee.getLength())-1);i++)
//{
//NodeList Name1=(NodeList) Name.item(i);
//NodeList Surname1=(NodeList) Surname.item(i);
//
//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);
StreamResult result = new StreamResult(out);
try {
tFormer.transform(source, result);
} catch (TransformerException e) {
e.printStackTrace();
}
}
// public void setParameter(Map arg0) {
//}
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
}
}
Thanks and Regards]
joy
Message was edited by: Atanu MAZUMDAR
User | Count |
---|---|
83 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.