on 07-18-2012 2:25 PM
Hi Group,
In one of the requirements,we are getting the below incoming SOAP request message and we need to
change the XML tags ,the whole XML message is coming as string in Message tag ,can any body help how we can achieve this using DOM parser in JAVA.
The input XML from MONI looks like below:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <!-- Inbound Message -->
- <MT_REG xmlns="http://pitest.com/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Message><IntData><SPANs><SPAN> <FileSeq>12321321313</FileSeq> <OriginalFileSeq>1233333</OriginalFileSeq> <MCore>1012405252171</MCore> <Supplier>ENDE</Supplier> </SPAN>;</SPANs> </IntData></Message>
</MT_REG>
but when we are trying to open in Wordpad/note pad message looks like below
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!-- Technical Routing -->
<MT_REG xmlns='http://pitest.com/jmswebservice' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<Message><IntData><SPANs><SPAN>
<FileSeq></FileSeq>
<OriginalFileSeq></OriginalFileSeq>
<MPANCore>1012405252171</MPANCore>
<Supplier>INDE</Supplier>
</MPAN>;</MPANs>
</IntData></Message></MT_REG>
Can any body help with Java code achieve the below target from using above inbound message
<?xml version="1.0" encoding="UTF-8"?><ns0:MT_REG xmlns:ns0="urn://testout.com/reg">
<Int2Data>
<PANs>
<PAN>
<Seq>12321321313</Seq>
<Seq1>1233333</Seq1>
<Core>1012405252171</Core>
<Sup>ENDE</Sup>
</Int2Data>
</ns0:MT_REG>
Hi Group,
Can any body help for my above query
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rajiv,
How are you? Its a long time I noticed your post again.
Here is the java code you need for PI 7.1 or above.
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.NodeList;
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 DomParser extends AbstractTransformation {
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
try
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builderel=factory.newDocumentBuilder();
Document docIn=builderel.parse(in);
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transform=tf.newTransformer();
Element root,child1,child2,child3,child4=null;
Document docOut=builderel.newDocument();
root=docOut.createElement("ns0:MT_REG");
root.setAttribute("xmlns:ns0","urn://testout.com/reg");
NodeList list,list1,list2,list3;
int i,l,j,l1,l2,l3,k,m;
final String sourceNodeList[]={"IntData","SPANs","SPAN","FileSeq",
"OriginalFileSeq","MCore","Supplier"};
final String TargetNodeList[]={"Int2Data","PANs","PAN","Seq",
"Seq1","Core","Sup"};
list=docIn.getElementsByTagName(sourceNodeList[0]);
l=list.getLength();
for(i=0;i<l;++i)
{
list1=list.item(i).getChildNodes();
l1=list1.getLength();
child1=docOut.createElement(TargetNodeList[0]);
for(j=0;j<l1;++j)
{
if(list1.item(j).getNodeName().equals(sourceNodeList[1]))
{
child2=docOut.createElement(TargetNodeList[1]);
list2=list1.item(j).getChildNodes();
l2=list2.getLength();
for(k=0;k<l2;++k)
{
if(list2.item(j).getNodeName().equals(sourceNodeList[2]))
{
child3=docOut.createElement(TargetNodeList[2]);
list3=list2.item(k).getChildNodes();
l3=list3.getLength();
for(m=0;m<l3;++m)
{
if(list3.item(m).getNodeName().equals(sourceNodeList[3]))
{
child4=docOut.createElement(TargetNodeList[3]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
else if(list3.item(m).getNodeName().equals(sourceNodeList[4]))
{
child4=docOut.createElement(TargetNodeList[4]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
else if(list3.item(m).getNodeName().equals(sourceNodeList[5]))
{
child4=docOut.createElement(TargetNodeList[5]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
else if(list3.item(m).getNodeName().equals(sourceNodeList[6]))
{
child4=docOut.createElement(TargetNodeList[6]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
}
child2.appendChild(child3);
}
}
child1.appendChild(child2);
}
}
root.appendChild(child1);
}
docOut.appendChild(root);
transform.transform(new DOMSource(docOut), new StreamResult(out));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileInputStream in=new FileInputStream("C:\\apps\\test\\inputSCN.xml");
FileOutputStream out=new FileOutputStream("C:\\apps\\test\\outputSCN.xml");
DomParser genFormat=new DomParser();
genFormat.execute(in,out);
}
}
Hope this solves your problem.
Regards
Anupam
Hi Anupam,
Thank you for your response,but I tried to run the above code with attached files,but its not generating expected output.
Input
-----------------
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!-- Technical Routing -->
<MT_REG xmlns='http://pitest.com/' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<Message><IntData><SPANs><SPAN>
<FileSeq>12321321313</FileSeq>
<OriginalFileSeq>1233333</OriginalFileSeq>
<MCore>1012405252171</MCore>
<Supplier>ENDE</Supplier>
</SPAN>;</SPANs>
</IntData></Message></MT_REG>
Output
---------------
<?xml version="1.0" encoding="UTF-8"?><ns0:MT_REG xmlns:ns0="urn://testout.com/reg"/>
Hi Rajiv,
Please confirm the following.
1. input to code was
file name:- inputSCN.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <!-- Inbound Message
-->
<MT_REG xmlns="http://pitest.com/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Message>
<IntData>
<SPANs>
<SPAN>
<FileSeq>12321321313</FileSeq>
<OriginalFileSeq>1233333</OriginalFileSeq>
<MCore>1012405252171</MCore>
<Supplier>ENDE</Supplier>
</SPAN>
</SPANs>
</IntData>
</Message>
</MT_REG>
and the required output was
outputSCN.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ns0:MT_REG xmlns:ns0="urn://testout.com/reg">
<Int2Data>
<PANs>
<PAN>
<Seq>12321321313</Seq>
<Seq1>1233333</Seq1>
<Core>1012405252171</Core>
<Sup>ENDE</Sup>
</PAN>
</PANs>
</Int2Data>
</ns0:MT_REG>
Then the code will definately work. I have checked it again now.
In case the input XML is different please upload the exact input xml you are providing to the code.
Please Right click on SXMB MONI and click view payload. Save this file as xml and upload the same in SCN.
Regards
Anupam
HI Anupam,
I completely agree with you,but as I said this is strange problem I am facing,the attached XML when I opened in internet explorer it shows XML tags ,but the same file when I am opening in word pad it is showing as below,your code absolutely fine when file is with XML tags,but for the attached file its not working.
The xml message within Intdata tag coming as String in SOAP request,might be because of this its not working
Hi Rajiv,
I had tough time figuring solution to this.
Anyways here it is
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
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.NodeList;
import org.xml.sax.InputSource;
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 DomParser extends AbstractTransformation {
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
try
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builderel=factory.newDocumentBuilder();
Document docIn=builderel.parse(in);
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transform=tf.newTransformer();
Element root,child1,child2,child3,child4=null;
Document docOut=builderel.newDocument();
Element srcRootNode;
srcRootNode=(Element) docIn.getElementsByTagName("Message").item(0);
String s=srcRootNode.getTextContent();
Document document = builderel.parse( new InputSource(new StringReader( s) ) );
docIn=document;
root=docOut.createElement("ns0:MT_REG");
root.setAttribute("xmlns:ns0","urn://testout.com/reg");
NodeList list,list1,list2,list3;
int i,l,j,l1,l2,l3,k,m;
final String sourceNodeList[]={"IntData","SPANs","SPAN","FileSeq",
"OriginalFileSeq","MCore","Supplier"};
final String TargetNodeList[]={"Int2Data","PANs","PAN","Seq",
"Seq1","Core","Sup"};
list=docIn.getElementsByTagName(sourceNodeList[0]);
l=list.getLength();
for(i=0;i<l;++i)
{
list1=list.item(i).getChildNodes();
l1=list1.getLength();
child1=docOut.createElement(TargetNodeList[0]);
for(j=0;j<l1;++j)
{
if(list1.item(j).getNodeName().equals(sourceNodeList[1]))
{
child2=docOut.createElement(TargetNodeList[1]);
list2=list1.item(j).getChildNodes();
l2=list2.getLength();
for(k=0;k<l2;++k)
{
if(list2.item(j).getNodeName().equals(sourceNodeList[2]))
{
child3=docOut.createElement(TargetNodeList[2]);
list3=list2.item(k).getChildNodes();
l3=list3.getLength();
for(m=0;m<l3;++m)
{
if(list3.item(m).getNodeName().equals(sourceNodeList[3]))
{
child4=docOut.createElement(TargetNodeList[3]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
else if(list3.item(m).getNodeName().equals(sourceNodeList[4]))
{
child4=docOut.createElement(TargetNodeList[4]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
else if(list3.item(m).getNodeName().equals(sourceNodeList[5]))
{
child4=docOut.createElement(TargetNodeList[5]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
else if(list3.item(m).getNodeName().equals(sourceNodeList[6]))
{
child4=docOut.createElement(TargetNodeList[6]);
child4.setTextContent(list3.item(m).getTextContent());
child3.appendChild(child4);
}
}
child2.appendChild(child3);
}
}
child1.appendChild(child2);
}
}
root.appendChild(child1);
}
docOut.appendChild(root);
transform.transform(new DOMSource(docOut), new StreamResult(out));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileInputStream in=new FileInputStream("C:\\apps\\test\\test12.xml");
FileOutputStream out=new FileOutputStream("C:\\apps\\test\\outputSCN.xml");
DomParser genFormat=new DomParser();
genFormat.execute(in,out);
}
}
Hope this helps.
Regards
Anupam
User | Count |
---|---|
89 | |
10 | |
9 | |
9 | |
9 | |
6 | |
6 | |
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.