cancel
Showing results for 
Search instead for 
Did you mean: 

Replace the namespace using java mapping

Former Member
0 Kudos

Hi friends,

i need java mapping to replace the namespace.

My payload is:

<?xml version="1.0" encoding="UTF-8"?>

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns0:Message1>

<ns0:Message2>

<ns2:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns2="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns2:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>

I want like this:


<?xml version="1.0" encoding="UTF-8"?>

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns0:Message1>

<ns0:Message2>

<ns1:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns1="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns1:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>


Note: I tried  AF_Modules/XMLAnonymizerBean in my receiver channel/Sender channel but it is not working so that i need use java mapping to replace the namespace.


Please advice...




Regards

Ravi

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ravi,

                  Please try this code


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.NamedNodeMap;

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 ReplacePrefix  extends AbstractTransformation{

  /**

  * @param args

  * @throws FileNotFoundException

  * @throws StreamTransformationException

  */

 

  @Override

  public void transform(TransformationInput arg0, TransformationOutput arg1)

  throws StreamTransformationException {

  // TODO Auto-generated method stub

  execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());

  }

    public void execute(InputStream in, OutputStream out) throws StreamTransformationException

    {

    try

    {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.parse(in);

    Document docOut = docBuilder.newDocument();

    Node SourceRootNode=doc.getDocumentElement();

  

    Node targetRootNode=docOut.importNode(SourceRootNode,true);

  

    docOut.appendChild(targetRootNode);

    NodeList l=docOut.getElementsByTagName("*");

    String ns="ns2";

    for(int i=0;i<l.getLength();++i)

    {

    System.out.println(l.item(i).getNodeName());

    if(l.item(i).getNodeName().indexOf(ns)>=0)

    {

    Element temp=(Element) l.item(i);

    NamedNodeMap attributes = l.item(i).getAttributes();

       for (int a = 0; a < attributes.getLength(); a++) {

         Node theAttribute = attributes.item(a);

         System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

         if(theAttribute.getNodeName().indexOf("ns2")>=0)

         {

          temp.removeAttribute(theAttribute.getNodeName());

         }

        

       }

    docOut.renameNode(l.item(i),"urn:sap-com:document:sap:rfc:functions",l.item(i).getNodeName().replaceAll("ns2", "ns1"));

    }

    }

    // write the content into xml file

    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(docOut);

    StreamResult result = new StreamResult(out);

    // Output to console for testing

    // StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

          

    }

    catch(Exception e)

    {

    e.printStackTrace();

    throw new StreamTransformationException(e.getMessage());

    }

    }

}

input

------


<?xml version="1.0" encoding="UTF-8"?>

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010 >

</ns0:Message1>

<ns0:Message2>

<ns2:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns2="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns2:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>

Output

---------


<?xml version="1.0" encoding="UTF-8" standalone="no"?><ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010>

</ns0:Message1>

<ns0:Message2>

<ns1:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns1="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns1:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>

Regards

Anupam

Former Member
0 Kudos

HI Anupam,

Thank you for providing the JavaCode,

while trying to compile this java code i am facing the some errors. so please provide .class file and .jar then easily we can use this jar file into our Operational mapping level.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ravi,

              Please try this  file.

After downloading the attachment please rename to ReplacePrefix.jar.

Regards

Anupam

Former Member
0 Kudos

Hi Anupam,

Thank you for your support.

still i am facing one error in PI. Please check.

Mapping "Company/Test.com/Test1_SendZASN_to_S_ReceiveASN_FAR_Test" failed to execute: MappingException: Parsing of the multi-structure document failed. , javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,9145]
Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#ElementPrefixUnbound?ns2&ns2:ZRFC_UPDATE_FARECON_DE...


Note:  we are sending Multiple IDOC's like....

<?xml version="1.0" encoding="UTF-8"?>

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010>

</ns0:Message1>

<ns0:Message2>

<ns2:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns2="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns2:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>



This above payload transfer to...



<?xml version="1.0" encoding="UTF-8"?>

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ASC856_004010>

<S_ISA>

</S_ISA>

</ASC856_004010>

<ASC856_004010>

<S_ISA>

</S_ISA>

</ASC856_004010>

<ASC856_004010>

<S_ISA>

</S_ISA>

</ASC856_004010>

</ns0:Message1>

<ns0:Message2>

<ns1:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns1="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns1:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>



is it possible in java mapping, if yes, then please let us know.


Thanks

Ravi

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ravi,

              This is possible


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.NamedNodeMap;

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 ReplacePrefix  extends AbstractTransformation{

  /**

  * @param args

  * @throws FileNotFoundException

  * @throws StreamTransformationException

  */

  public static void main(String[] args) throws FileNotFoundException, StreamTransformationException {

  // TODO Auto-generated method stub

  String zipFile="C:/apps/ravi.xml";

  //zipFile="C:\\apps\\ravixmlout.xml";

  String Output="C:\\apps\\ravixmlout.xml";

  InputStream in=new FileInputStream(zipFile);

        OutputStream out=new FileOutputStream(Output);

        ReplacePrefix r=new ReplacePrefix();

        r.execute(in,out);

  }

  @Override

  public void transform(TransformationInput arg0, TransformationOutput arg1)

  throws StreamTransformationException {

  // TODO Auto-generated method stub

  execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());

  }

    public void execute(InputStream in, OutputStream out) throws StreamTransformationException

    {

    try

    {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.parse(in);

    Document docOut = docBuilder.newDocument();

    Node SourceRootNode=doc.getDocumentElement();

  

    Node targetRootNode=docOut.importNode(SourceRootNode,true);

  

    docOut.appendChild(targetRootNode);

    NodeList l=docOut.getElementsByTagName("*");

    String ns="ns2";

    for(int i=0;i<l.getLength();++i)

    {

    System.out.println(l.item(i).getNodeName());

    if(l.item(i).getNodeName().indexOf(ns)>=0)

    {

    Element temp=(Element) l.item(i);

    NamedNodeMap attributes = l.item(i).getAttributes();

       for (int a = 0; a < attributes.getLength(); a++) {

         Node theAttribute = attributes.item(a);

         System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

         if(theAttribute.getNodeName().indexOf("ns2")>=0 )

         {

          String atValue=theAttribute.getNodeValue();

          temp.removeAttribute(theAttribute.getNodeName());

        

          docOut.renameNode(l.item(i),atValue,l.item(i).getNodeName().replaceAll("ns2", "ns1"));

         }

        

       }

  

    }

    else if(l.item(i).getNodeName().indexOf("ns1")>=0)

    {

    Element temp=(Element) l.item(i);

    NamedNodeMap attributes = l.item(i).getAttributes();

       for (int a = 0; a < attributes.getLength(); a++) {

         Node theAttribute = attributes.item(a);

         System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

         if(theAttribute.getNodeName().indexOf("ns1")>=0 )

         {

          temp.removeAttribute(theAttribute.getNodeName());

          String newName=l.item(i).getNodeName().replaceAll("ns1:", "");

          docOut.renameNode(l.item(i),"",newName);

         }

        

       }

  

    }

    }

    // write the content into xml file

    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(docOut);

    StreamResult result = new StreamResult(out);

    // Output to console for testing

    // StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

          

    }

    catch(Exception e)

    {

    e.printStackTrace();

    throw new StreamTransformationException(e.getMessage());

    }

    }

}

input

---------


<?xml version="1.0" encoding="UTF-8"?>

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010>

<ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">

<S_ISA>

</S_ISA>

</ns1:ASC856_004010>

</ns0:Message1>

<ns0:Message2>

<ns2:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns2="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns2:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>

Output

----------


<?xml version="1.0" encoding="UTF-8" standalone="no"?><ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ASC856_004010>

<S_ISA>

</S_ISA>

</ASC856_004010>

<ASC856_004010>

<S_ISA>

</S_ISA>

</ASC856_004010>

<ASC856_004010>

<S_ISA>

</S_ISA>

</ASC856_004010>

</ns0:Message1>

<ns0:Message2>

<ns1:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns1="urn:sap-com:document:sap:rfc:functions">

<T_FA_RECON>

<item>

</item>

</T_FA_RECON>

</ns1:ZRFC_UPDATE_FARECON_DETAILS>

</ns0:Message2>

</ns0:Messages>

Please download the attachment and rename it to ReplacePrefix.jar.

Regards

Anupam

Former Member
0 Kudos

Hi Anupam,

Thank you,

what you given that code as replaced as ns1, but i am facing below error. and <ns1:ASC856_004010 xmlns:ns1="company/ANSI_X12_V4010">  this is not replaced as like <ASC856_004010>.  so please suggest me.

Error:

Mapping "COMPANY_EDI/Test.com/Test_SendZASN_to_S_ReceiveASN_FAR_Test" failed to execute: MappingException: Parsing of the multi-structure document failed. , javax.xml.stream.XMLStreamException: prefix ns1 has been already bound to urn:sap-com:document:sap:rfc:functions. Rebinding it to Company_APPL/ANSI_X12_V4010 is an error.


Transmitting the message to endpoint <local> using connection File_http://sap.com/xi/XI/System failed, due to: com.sap.aii.af.service.mapping.MappingException: Parsing of the multi-structure document failed.



Thanks

Ravi

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ravi,

Can you zip and upload the actual payload which is failing ?

Regards

Anupam

Former Member
0 Kudos

Hi Anupam,

Thankyou for your support..

i got  the solution when i modify the XSD file.

Thankyou all.......

Answers (2)

Answers (2)

former_member190293
Active Contributor
0 Kudos

Hi Ravi!

Actually it's the bad idea to use the same prefix for different namespaces within one xml document, though it's possible technically.

Couldn't you explain why do you need it?

Regards, Evgeniy.

former_member186851
Active Contributor
0 Kudos

Eve ur finding differences in XML?

former_member190293
Active Contributor
0 Kudos

Hi Raghuraman!

<ns2:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns2="urn:sap-com:document:sap:rfc:functions">


<ns1:ZRFC_UPDATE_FARECON_DETAILS xmlns:ns1="urn:sap-com:document:sap:rfc:functions">


Regards, Evgeniy.

Former Member
0 Kudos

Hi Evg,

Sorry for the delay reply,

In My Scenario IDOC file is coming from SAP r/3 and send to Target. In Target level we are using Multi Mapping so One file is going to Target. and another file is Updating to R/3(i.e ZRFC_UPDATE_FARECON_DETAILS).

So that the namespace is coming "ns1" it is updating to R/3. if "ns2" it is not updating to R/3.

For this purpose i am using  Anonimizer bean, but it is not working in my scenario level. so that i am choosing javamapping.

please suggest me if any thing wrong.

former_member190293
Active Contributor
0 Kudos

Hi Ravi!

It's the strange issue that namespace prefix prevents RFC from working.

In such case I'd suggest just to swap prefixes instead of using the same prefix for different namespaces.

Try XMLAnonymizerBean with parameter anonymizer.acceptNamespaces and value "http://sap.com/xi/XI/SplitAndMerge ns0 company/ANSI_X12_V4010 ns2 urn:sap-com:document:sap:rfc:functions ns1"


Regards, Evgeniy.

former_member186851
Active Contributor
0 Kudos

Hello Ravi,

Not seeing any difference in target XML.

For NS change check the below link