on 08-12-2016 11:33 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
82 | |
10 | |
10 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.