title: Invalid byte 2 of 2-byte UTF-8 sequence in SAP PI mapping
body: <p>This is SAP PI scenario. The message is exchanged between 2 systems.  Source System: IPOS  Target system: ECC..  Once the message reaches PI system an xml corresponding to IPOS is generated. I have to map the IPOS structure to ECC structure.  Here I am using DOM parser..   I am using main() for testing purpose.</p>
  
  <pre><code>public static void main (String str[]) throws FileNotFoundException, StreamTransformationException { 
  
      ZcreateHomeDelivery obj = new ZcreateHomeDelivery();
  
      try { 
  
  
      InputStream inputStream  = new FileInputStream("C:/Users/XYZ/workspace/input.xml");
  
      FileOutputStream newOut = new FileOutputStream("C:/Users/XYZ/workspace/output.xml");  
      obj.execute(inputStream, newOut);
      }catch (Exception e ) { 
          e.printStackTrace();
      }}
  </code></pre>
  
  <p>I have imported all the required packages..</p>
  
  <pre><code>import com.sap.aii.mapping.api.AbstractTrace;
  import com.sap.aii.mapping.api.StreamTransformation;
  </code></pre>
  
  <p>The problem I am facing is when I execute the code, I am getting error as</p>
  
  <pre><code>com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence.
  at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
  at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
  at com.sap.nw.rr.hd.ZcreateHomeDelivery.execute(ZcreateHomeDelivery.java:85)
  at com.sap.nw.rr.hd.ZcreateHomeDelivery.main(ZcreateHomeDelivery.java:801)
  java.lang.NullPointerException
  at com.sap.nw.rr.hd.ZcreateHomeDelivery.execute(ZcreateHomeDelivery.java:101)
  at com.sap.nw.rr.hd.ZcreateHomeDelivery.main(ZcreateHomeDelivery.java:801)
  </code></pre>
  
  <p>The solution that I have tried is </p>
  
  <pre><code>try
      {
          TransformerFactory tFactory = TransformerFactory.newInstance();
          Transformer transformer = tFactory.newTransformer();
          transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");// added
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
          DOMSource source = new DOMSource(outdoc);
          StreamResult result = new StreamResult(outputStream);
          transformer.transform(source, result);
      } catch (Exception e) {
          e.printStackTrace();
          throw new StreamTransformationException("Mapping failed:Check input file", e);
      }
  </code></pre>
  
  <p>*added(Commented in the above code) to resolve the issue- But it dint help.  I have to add UTF-8 , But where to add it ?</p>
  
  <p>This is resolved : solution is<br>
  1)Open the <code>xml</code> in notepad<br>
  2)Make sure you dont have extra space at the beginning and end of the document.<br>
  3)Select File -> Save As<br>
  4)select save as type -> All files<br>
  5)Enter file name as <code>filename.xml</code><br>
  6)select Encoding - <code>UTF-8</code>-> Click Save  </p>