cancel
Showing results for 
Search instead for 
Did you mean: 

XML DOM Parser

former_member188791
Participant
0 Kudos

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>&#60;IntData&#62;&#60;SPANs&#62;&#60;SPAN&#62;

  &#60;FileSeq&#62;&#60;/FileSeq&#62;

  &#60;OriginalFileSeq&#62;&#60;/OriginalFileSeq&#62;

  &#60;MPANCore&#62;1012405252171&#60;/MPANCore&#62;

  &#60;Supplier&#62;INDE&#60;/Supplier&#62;

  &#60;/MPAN&#62;;&#60;/MPANs&#62;

&#60;/IntData&#62;</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>

Accepted Solutions (1)

Accepted Solutions (1)

former_member188791
Participant
0 Kudos

Hi Group,

Can any body help for my above query

anupam_ghosh2
Active Contributor
0 Kudos

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

former_member188791
Participant
0 Kudos

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>&#60;IntData&#62;&#60;SPANs&#62;&#60;SPAN&#62;

  &#60;FileSeq&#62;12321321313&#60;/FileSeq&#62;

  &#60;OriginalFileSeq&#62;1233333&#60;/OriginalFileSeq&#62;

  &#60;MCore&#62;1012405252171&#60;/MCore&#62;

  &#60;Supplier&#62;ENDE&#60;/Supplier&#62;

  &#60;/SPAN&#62;;&#60;/SPANs&#62;

&#60;/IntData&#62;</Message></MT_REG>

Output

---------------

<?xml version="1.0" encoding="UTF-8"?><ns0:MT_REG xmlns:ns0="urn://testout.com/reg"/>

anupam_ghosh2
Active Contributor
0 Kudos

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

former_member188791
Participant
0 Kudos

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

anupam_ghosh2
Active Contributor
0 Kudos

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

former_member188791
Participant
0 Kudos

Hi Anupam,

Gr8,its working now,Thank lot.

Answers (0)