on 01-02-2016 4:43 PM
Hi All,
I am trying java mapping to concate two fields. Below is my code but I am getting error at line - String document_exit. Please help
Code -
public class conc extends AbstractTransformation {
public void transform(TransformationInput input, TransformationOutput output)
throws StreamTransformationException {
String RESULT = new String();
InputStream in = input.getInputPayload().getInputStream();
OutputStream out = output.getOutputPayload().getOutputStream();
try {
//Create DOM parser
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
NodeList n1 = doc.getElementsByTagName("Emp_Name");
NodeList n2 = doc.getElementsByTagName("Emp_Surname");
String nv1 = doc.getNodeName();
String nv2 = doc.getNodeName();
if (nv1.equals(n1) && nv2.equals(n2))
RESULT = nv1.concat(nv2);
String document_exit = "<?xml version="1.0" encoding="UTF-8"?>" +
"<ns0:MT_SJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi>" +
"<Records> <EmployeeID/> <RESULT>" + RESULT + "</RESULT> </Records>";
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(document_exit));
}catch (Exception exception) {
getTrace().addDebugMessage(exception.getMessage());
throw new StreamTransformationException(exception.toString());
}
}
}
BR,
Rashmi
Thank you all. I am getting correct output. But again I got few doubts -
1. I am unable to get tree view of my output structure. Why?
2. Initially I used below line to generate by output but it didnt work-
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(RESULT));
whereas when I used -
out.write(document_exit.getBytes());
This gave me output.
What is difference between these 2 code lines?
And which line should be used when?
BR,
Rashmi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rashmi,
You can use below java mapping, instead of hard coding the target xml you can use below code to generate the target xml, and also it works for multiple records.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 org.w3c.dom.NodeList;
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 ConcatJavaMap extends AbstractTransformation {
public static void main(String[] args) throws StreamTransformationException, FileNotFoundException {
ConcatJavaMap concat = new ConcatJavaMap();
FileInputStream input = new FileInputStream("input.xml");
FileOutputStream output = new FileOutputStream("output.xml");
concat.execute(input, output);
}
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
execute(transformationInput.getInputPayload().getInputStream(), transformationOutput.getOutputPayload().getOutputStream());
}
public void execute(InputStream inputStream, OutputStream outputStream) throws StreamTransformationException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
factory.setIgnoringElementContentWhitespace(true);
Document inputDoc = builder.parse(inputStream);
Document outputDoc = builder.newDocument();
Element outMsgType = outputDoc.createElement("ns0:MT_RJava_Concanat");
outMsgType.setAttribute("xmlns:ns0", "urn:javaMapping.com/Rashmi");
outputDoc.appendChild(outMsgType);
NodeList recordsNodeList = inputDoc.getElementsByTagName("Records");
for (int i = 0; i < recordsNodeList.getLength(); i++) {
Node recordsNode = recordsNodeList.item(i);
if (recordsNode.getNodeType() == Node.ELEMENT_NODE) {
Element recordsSource = (Element) recordsNode;
Element recordsTarget = outputDoc.createElement("Records");
outMsgType.appendChild(recordsTarget);
// Employee ID
Element empElement = outputDoc.createElement("EmployeeID");
empElement.setTextContent(recordsSource.getElementsByTagName("EmployeeID").item(0).getTextContent());
recordsTarget.appendChild(empElement);
// Result
Element result = outputDoc.createElement("RESULT");
String name = recordsSource.getElementsByTagName("Emp_Name").item(0).getTextContent();
String surname = recordsSource.getElementsByTagName("Emp_Surname").item(0).getTextContent();
String fullName = name + " " + surname;
result.setTextContent(fullName);
recordsTarget.appendChild(result);
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
DOMSource source = new DOMSource(outputDoc);
StreamResult result = new StreamResult(outputStream);
transformer.transform(source, result);
} catch (Exception e) {
getTrace().addDebugMessage(e.getMessage());
throw new StreamTransformationException(e.getMessage());
}
}
}
Result:
Regards,
Praveen.
Thanks Praveen. I am new in java mapping and at very beginning level and trying to understand the nodes nodelist etc things here.
While going through your code I got stuck at below line It will be great if you can explain me this so that I can scan this code further
if (recordsNode.getNodeType() == Node.ELEMENT_NODE) {
Element recordsSource = (Element) recordsNode;
BR,
Rashmi
Hi All,
Just to update you, I am using below code. Now my fields are getting concatenated but one of my fields from source not getting populated at target side -
Test -
I have 2 queries here -
1. How to get field EmployeeID at target side?
2. How to get data as XML at target side?
Used Code -
try {
//Create DOM parser
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
NodeList nameList = doc.getElementsByTagName("Emp_Name");
if (nameList.getLength() > 0)
Name = nameList.item(0).getTextContent();
NodeList surNameList = doc.getElementsByTagName("Emp_Surname");
if (surNameList.getLength() > 0)
Surname = surNameList.item(0).getTextContent();
RESULT = Name + " " + Surname;
out.write(RESULT.getBytes());
BR,
Rashmi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rashmi,
1. How to get field EmployeeID at target side?
You can get it like the other tags that you are dealing now:
String employeeID = new String():
NodeList empList = doc.getElementsByTagName("EmployeeID");
if (empList.getLength() > 0)
employeeID = empList.item(0).getTextContent();
2. How to get data as XML at target side?
You have done this in before posts:
String document_exit = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi>" + "<Records> <EmployeeID>" + employeeID + "</EmployeeID><RESULT>" + RESULT + "</RESULT> </Records> </ns0:MT_RJava_Concanat>";
out.write(document_exit.getBytes());
Regards,
Below is my source structure.. Trying to concate name and surname -
Modified Code -
/**
*
*/
package concat;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilderFactory;
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.NodeList;
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;
/**
* @author rashjoshi
*
*/
public class Conc extends AbstractTransformation {
public void transform(TransformationInput input, TransformationOutput output)
throws StreamTransformationException {
String RESULT = new String();
InputStream in = input.getInputPayload().getInputStream();
OutputStream out = output.getOutputPayload().getOutputStream();
try {
//Create DOM parser
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
NodeList n1 = doc.getElementsByTagName("Emp_Name");
NodeList n2 = doc.getElementsByTagName("Emp_Surname");
String nv1 = doc.getNodeName();
String nv2 = doc.getNodeName();
if (nv1.equals(n1) && nv2.equals(n2))
RESULT = nv1.concat(nv2);
String document_exit = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi>" + "<Records> <EmployeeID/> <RESULT>" + RESULT + "</RESULT> </Records>";
out.write(document_exit.getBytes());
//output.write(document_exit.getBytes());
// TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(document_exit));
}
catch (Exception exception) {
getTrace().addDebugMessage(exception.getMessage());
throw new StreamTransformationException(exception.toString());
}
}
}
BR,
Rashmi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Rashmi,
NodeList nameList = doc.getElementsByTagName("Emp_Name");
if (nameList.getLength() > 0)
Name = nameList.item(0).getTextContent();
NodeList surNameList = doc.getElementsByTagName("Emp_Surname");
if (surNameList.getLength() > 0)
Surname = surNameList.item(0).getTextContent();
RESULT = Name + " " + Surname;
Use the above code for conatenating and check,From my observation,I am seeing your concatenation Root nodes instead of data.
Hi Rashmi,
Can you please provide a sample input and output xml.
This will help forum members to analyse what went wrong.
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Rashmi,
Your document exit XML namespace defintion is wrong, Try changing it as below
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ns0:MT_SJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi>" + "<Records> <EmployeeID/> <RESULT>" + RESULT + "</RESULT> </Records>";
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am getting below error after tried with your string line -
Application mapping program concat/Conc throws a stream transformation exception: javax.xml.transform.TransformerException: java.io.FileNotFoundException: <?xml version="1.0" encoding="UTF-8"?><ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com\Rashmi><Records> <EmployeeID\> <RESULT><\RESULT> <\Records> (The filename, directory name, or volume label syntax is incorrect)
See error logs for details
Hi Inaki,
Below is my test result from ESR -
Source Data -
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_SJava_Concanat xmlns:ns0="urn:javaMapping.com/Rashmi">
<Records>
<EmployeeID>123</EmployeeID>
<Emp_Name>Rashmi</Emp_Name>
<Emp_Surname>Joshi</Emp_Surname>
</Records>
</ns0:MT_SJava_Concanat>
Target Data -
<?xml version="1.0" encoding="UTF-8"?><ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi><Records> <EmployeeID/> <RESULT></RESULT> </Records> </ns0:MT_RJava_Concanat>
Where is my data???
BR,
Rashmi
Hi Rashmi,
In your code change the below code to the one marked in bold.
Remove the below lines of code
NodeList n1 = doc.getElementsByTagName("Emp_Name");
NodeList n2 = doc.getElementsByTagName("Emp_Surname");
String nv1 = doc.getNodeName();
String nv2 = doc.getNodeName();
if (nv1.equals(n1) && nv2.equals(n2))
RESULT = nv1.concat(nv2);
and replace with below code:
NodeList n1 = doc.getElementsByTagName("Emp_Name");
NodeList n2 = doc.getElementsByTagName("Emp_Surname");
//String nv1 = doc.getNodeName();
//String nv2 = doc.getNodeName();
//if (nv1.equals(n1) && nv2.equals(n2))
RESULT = n1.item(0).getTextContent().concat(n2.item(0).getTextContent());
Best Regards,
Rohit
Hi Rohit,
While testing my map I am getting below result.... Not getting values in structure -
<?xml version="1.0" encoding="UTF-8"?><ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi><Records> <EmployeeID/> <RESULT></RESULT> </Records> </ns0:MT_RJava_Concanat>
Below is my modified code -
public void transform(TransformationInput input, TransformationOutput output)
throws StreamTransformationException {
String RESULT = new String();
InputStream in = input.getInputPayload().getInputStream();
OutputStream out = output.getOutputPayload().getOutputStream();
try {
//Create DOM parser
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
NodeList n1 = doc.getElementsByTagName("Emp_Name");
NodeList n2 = doc.getElementsByTagName("Emp_Surname");
// String nv1 = doc.getNodeName();
// String nv2 = doc.getNodeName();
// if (nv1.equals(n1) && nv2.equals(n2))
// RESULT = nv1.concat(nv2);
n1.item(0).getTextContent().concat(n2.item(0).getTextContent());
String document_exit = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi>" + "<Records> <EmployeeID/> <RESULT>" + RESULT + "</RESULT> </Records> </ns0:MT_RJava_Concanat>";
out.write(document_exit.getBytes());
BR,
Rashmi
Hi Rashmni,
As Praveen wrote you have to access to the tag values with:
xxx.item(0).getTextContent();
Also, i used to have this method:
public String obtenerTagValue (Document xml, String tag){
NodeList nlEtiqueta = xml.getElementsByTagName(tag);
return nlEtiqueta.item(0).getFirstChild().toString();
}
Try to set the variable Trace and do the method addInfo with the value of the variables.
Regards,
Hi Rashmi,
You can use below java mapping.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
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 ConcatJavaMap extends AbstractTransformation {
public static void main(String[] args) throws StreamTransformationException, FileNotFoundException {
ConcatJavaMap concat = new ConcatJavaMap();
FileInputStream input = new FileInputStream("input.xml");
FileOutputStream output = new FileOutputStream("output.xml");
concat.execute(input, output);
}
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
execute(transformationInput.getInputPayload().getInputStream(), transformationOutput.getOutputPayload().getOutputStream());
}
public void execute(InputStream input, OutputStream output) throws StreamTransformationException {
String name = "", surname = "";
String RESULT = new String();
try {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
NodeList nameList = doc.getElementsByTagName("Emp_Name");
if (nameList.getLength() > 0)
name = nameList.item(0).getTextContent();
NodeList surNameList = doc.getElementsByTagName("Emp_Surname");
if (surNameList.getLength() > 0)
surname = surNameList.item(0).getTextContent();
RESULT = name + " " + surname;
String document_exit = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<ns0:MT_SJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi>" + "<Records> <EmployeeID/> <RESULT>"
+ RESULT + "</RESULT> </Records>";
output.write(document_exit.getBytes());
} catch (Exception e) {
getTrace().addDebugMessage(e.getMessage());
throw new StreamTransformationException(e.getMessage());
}
}
}
Regards,
Praveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried to get my output with your provided code but my data not getting concatenated. I am just getting value of my document_exit in output of mapping -
Below is test result from ESR -
<?xml version="1.0" encoding="UTF-8"?><ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com/Rashmi><Records> <EmployeeID/> <RESULT></RESULT> </Records>
BR,
Rashmi
Hi Rashmi,
Try to insert '\' in the xml tag as <? Xml version=\"1.0\" encoding=\"utf-8\">
Br,
Manoj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
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.