cancel
Showing results for 
Search instead for 
Did you mean: 

Remove Doctype from cXML file

Akhil_Sun
Participant
0 Kudos
581

Hi All,

I need to remove the doctype from incoming cXML file. I read all the forums regarding this issue and decided to use the below link to solve my problem

http://scn.sap.com/thread/3184205

The above link specifies the java code for PI7.1 to remove doctype since I am using PI7.0 I modified the code

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

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.Node;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;

public class DeleteDtd implements StreamTransformation {
     
       public void execute(InputStream in, OutputStream out)
               throws StreamTransformationException {
          
          try
          {
               DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
               DocumentBuilder builderel=factory.newDocumentBuilder();
               /*input document in form of XML*/
               Document docIn=builderel.parse(in);
               /*document after parsing*/
               Document docOut=builderel.newDocument();
               TransformerFactory tf=TransformerFactory.newInstance();
               Transformer transform=tf.newTransformer();
               Node rootIn,rootOut;
               rootIn=docIn.getDocumentElement();
               rootOut=docOut.importNode(rootIn,true);
               docOut.appendChild(rootOut);
               transform.transform(new DOMSource(docOut), new StreamResult(out));
          }
          catch(Exception e)
          {
               e.printStackTrace();
          }
          
       }
       public static void main(String[] args) {
           try{
                DeleteDtd genFormat=new DeleteDtd();
                FileInputStream in=new FileInputStream("E:\\input.xml");
                FileOutputStream out=new FileOutputStream("E:\\output.xml");
                genFormat.execute(in,out);
           }
           catch(Exception e)
           {
                e.printStackTrace();
           }
      }
     public void setParameter(Map arg0) {
         // TODO Auto-generated method stub
        
    }
}


So when I run this code in eclipse its is removing the doctype but once I deployed in PI 7.0 it is throwing same error as empty document cannot be processed.

Can anyone please let me know what correction I need to make this code to run in PI 7.0 to remove doctype

<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/x.x.xxx/cXML.dtd">

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

I am not seeing in the code where you are editing or removing the DOCTYPE..instead of reading & handling the whole thing with DOM parser, read the data as input steam.

Search for the string "<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/x.x.xxx/cXML.dtd">"

& replace it with

<cXML SYSTEM "http://xml.cXML.org/schemas/cXML/x.x.xxx/cXML.dtd">

before writting to the output steam.

Regards,

Ashish

Akhil_Sun
Participant
0 Kudos

Hi Ashish,

I did that and it is working fine but i dont want to read it as a string and use replace

Below is the code which is working perfectly for me but I dont have to keep string as "<!doctype";

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
public class RemoveDocMap implements StreamTransformation {
   private Map map = null;
   private AbstractTrace trace = null;
   public void setParameter(Map arg0) {
      map = arg0;   // Store reference to the mapping parameters
      if (map == null) {
         this.map = new HashMap();
      }
   }
   public void execute(InputStream arg0, OutputStream arg1)
         throws StreamTransformationException {
      String line = null;
      BufferedReader br = new BufferedReader(new InputStreamReader(arg0));
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(arg1));
      // Get reference to mapping trace
      trace = (AbstractTrace)map.get(StreamTransformationConstants.MAPPING_TRACE);
      trace.addInfo("Processing message");
     
      try {
         //Read all lines of input stream arg0
         while ((line=br.readLine()) != null) {
            //Write output to output stream arg1
          String sub = line;
          String find = "<!doctype";
          int ind1 = sub.toLowerCase().indexOf(find.toLowerCase());
          if(ind1>=0)
          {
           line = "";
          }
          bw.write(line);
         }
        
         bw.flush();
         br.close();
         bw.close();
      } catch (IOException ioe) {
         trace.addWarning("Could not process source message");
         throw new RuntimeException(ioe.getMessage());
      }
      trace.addInfo("Processing completed successfully");
   }
}