cancel
Showing results for 
Search instead for 
Did you mean: 

DOM Parsing concatenate help required

rasjoshi
Active Contributor
0 Kudos

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

Accepted Solutions (0)

Answers (7)

Answers (7)

rasjoshi
Active Contributor
0 Kudos

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

iaki_vila
Active Contributor
0 Kudos

Hi Rashmi,

I think the main problem was that you have to write the output stream in the variable TransformationOutput out, this variable is taken by PI mapping engine like java mapping output but you have used a StreamResult variable not linked by PI as java mapping output.

Regards.

former_member182412
Active Contributor
0 Kudos

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.

rasjoshi
Active Contributor
0 Kudos

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

former_member186851
Active Contributor
0 Kudos

Hello Rashmi,

You can refer the below link,It should be helpful for you

Java XML Parsers

rasjoshi
Active Contributor
0 Kudos

Thats very good site. If you have any more links for beggining level please share.

BR,

Rashmi

former_member182412
Active Contributor
0 Kudos

Hi Rashmi,

I am checking whether the node is element node if it is i am assigning it to element. normally node represents Element, Attribute, Text etc.

Regards,

Praveen.

former_member186851
Active Contributor
0 Kudos

Hello Rashmi,

You can refer the below links

XML Parsing for Java

XML DOM Tutorial

rasjoshi
Active Contributor
0 Kudos

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

iaki_vila
Active Contributor
0 Kudos

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,

former_member186851
Active Contributor
0 Kudos

Hello Rashmi,

change from

out.write(RESULT.getBytes());

to

out.write(document_exit.getBytes());

Since your document_exit contains the XML.

rasjoshi
Active Contributor
0 Kudos

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

former_member186851
Active Contributor
0 Kudos

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.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Rashmi,

                  Can you please provide a sample input and output xml.

This will help forum members to analyse what went wrong.

Regards

Anupam

former_member186851
Active Contributor
0 Kudos

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>";

rasjoshi
Active Contributor
0 Kudos

Hi Raghu,

Is my below line correct?



TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(document_exit));



BR,

Rashmi

rohit_j
Participant
0 Kudos

Hi Rashmi,

I don't think it is right.

You should be using the existing instance of "TransformationOutput" which is part of the parameter list "output".

This should be replaced with "output.write(document_exit.getBytes("UTF-8"));"


Best Regards,

Rohit

rasjoshi
Active Contributor
0 Kudos

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    

iaki_vila
Active Contributor
0 Kudos

H Rashmi,

You have to close the tag <ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com\Rashmi>

<?xml version="1.0" encoding="UTF-8"?><ns0:MT_RJava_Concanat xmlns:ns0=urn:javaMapping.com\Rashmi><Records> <EmployeeID\> <RESULT>

</ns0:MT_RJava_Concanat>

Regards

former_member186851
Active Contributor
0 Kudos

Hello Rashmi,

Please post the code which you using now after the changes,So that we can sort it out.

rasjoshi
Active Contributor
0 Kudos

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

rohit_j
Participant
0 Kudos

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

rasjoshi
Active Contributor
0 Kudos

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

iaki_vila
Active Contributor
0 Kudos

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,

former_member182412
Active Contributor
0 Kudos

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.

rasjoshi
Active Contributor
0 Kudos

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

manoj_khavatkopp
Active Contributor
0 Kudos

Hi Rashmi,

Try to insert '\'  in the xml tag as <? Xml version=\"1.0\" encoding=\"utf-8\">

Br,

Manoj