Show TOC

Background documentationExample Program IDoc Client Locate this document in the navigation structure

 

The following programming example shows the use of the IDoc class library for IDoc communication using SAP JCo 3.0 from the client side.

Syntax Syntax

IDoc Client

  1. package com.sap.conn.idoc.examples;
  2. import com.sap.conn.jco.*;
  3. import com.sap.conn.idoc.jco.*;
  4. import com.sap.conn.idoc.*;
  5. import java.io.*;
  6. public class IDocClientExample {
  7. 	public static void main(String[] args) {
  8. 		
  9. 		try
  10.         {
  11. 			String iDocXML = null;
  12. 			FileReader fileReader;
  13. 			try
  14. 			{
  15. 				fileReader = new FileReader("MyIDocDocumentAsXML.xml");
  16. 				BufferedReader br = new BufferedReader(fileReader);
  17. 				StringBuffer sb = new StringBuffer();
  18. 				String line;
  19. 				while ((line = br.readLine()) != null)
  20. 				{
  21. 					sb.append(line);
  22. 				}
  23. 				iDocXML = sb.toString();
  24. 				
  25. 				
  26. 				br.close();
  27. 				fileReader.close();
  28. 			}
  29. 			catch(Exception ex)
  30. 			{
  31. 				ex.printStackTrace();
  32. 			}
  33. 			
  34. 			// see configuration file BCE.jcoDestination provided in the installation directory.
  35.         	JCoDestination destination=JCoDestinationManager.getDestination("BCE"); 
  36.         	IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
  37.         	String tid = destination.createTID();
  38.         	IDocFactory iDocFactory = JCoIDoc.getIDocFactory();
  39.         	
  40.         	// a) create new idoc
  41.         	IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "MATMAS02");
  42.         	IDocSegment segment = doc.getRootSegment();
  43.         	segment = segment.addChild("E1MARAM");
  44.         	// and so on. See IDoc Specification .....
  45.         	JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
  46.         	
  47.         	
  48.         	// b) use existent xml file
  49.         	IDocXMLProcessor processor=iDocFactory.getIDocXMLProcessor();
  50.             IDocDocumentList iDocList=processor.parse(iDocRepository, iDocXML);
  51.             JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
  52.             destination.confirmTID(tid);
  53.             
  54.         }
  55.         catch(Exception e)
  56.         {
  57.         	e.printStackTrace();
  58.         }
  59.         System.out.print("program end");
  60. 	}
  61. }
End of the code.