title: SAP JCo parse XML to IDOC with field extensions
body: <p>i have some problems parsing an XML to IDOC using the SAP JCo library. I know I need a connection to the SAP system which is given.</p>
  
  <p>Here is my gradle test code :</p>
  
  <pre><code>    when:
      JCoIDocServer server = JCoIDoc.getServer(sapProperties.get("jco.server.progid"))
  
      IDocXMLProcessor xmlProcessor = JCoIDoc.getIDocFactory().getIDocXMLProcessor()
      IDocDocumentList iDocDocumentList = xmlProcessor.parse(server.getIDocRepository(), getClass().getResourceAsStream("/data/DEBMAS-406868135.XML"), IDocXMLProcessor.PARSE_IGNORE_UNKNOWN_FIELDS)
  </code></pre>
  
  <p>The connection works, but JCo finds inconsistency in my XML (exported from SAP, no manual changes!) which seems caused by SAP extension fields:</p>
  
  <pre><code>com.sap.conn.idoc.IDocParseException: (7) IDOC_ERROR_PARSE_FAILURE: IDoc type extension ZDEBMAS3 within the EDI_DC40 control record segment does not match the IDoc-XML root tag &lt;DEBMAS05&gt;:
  </code></pre>
  
  <p>state=READING_ENDTAG, charPosition=852, lineNumber=24, columnNumber=19</p>
  
  <p>As you can see I tried to use XML parser options which did not help. </p>
  
  <p>What do I have to do the extension fields are accepted by the IDocParser?</p>
  
  <p><strong>Update 1</strong>
  Here you can find the anonymized IDOC XML.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;DEBMAS05&gt;
      &lt;IDOC BEGIN="1"&gt;
          &lt;EDI_DC40 SEGMENT="1"&gt;
              &lt;TABNAM&gt;&lt;![CDATA[EDI_DC40]]&gt;&lt;/TABNAM&gt;
              &lt;MANDT&gt;100&lt;/MANDT&gt;
              &lt;DOCNUM&gt;0000000406868135&lt;/DOCNUM&gt;
              &lt;DOCREL&gt;750&lt;/DOCREL&gt;
              &lt;STATUS&gt;12&lt;/STATUS&gt;
              &lt;DIRECT&gt;1&lt;/DIRECT&gt;
              &lt;OUTMOD&gt;2&lt;/OUTMOD&gt;
              &lt;IDOCTYP&gt;DEBMAS05&lt;/IDOCTYP&gt;
              &lt;CIMTYP&gt;ZDEBMAS3&lt;/CIMTYP&gt;
              &lt;MESTYP&gt;&lt;![CDATA[Z_DEBMAS2]]&gt;&lt;/MESTYP&gt;
              &lt;SNDPOR&gt;XXX&lt;/SNDPOR&gt;
              &lt;SNDPRT&gt;LS&lt;/SNDPRT&gt;
              &lt;SNDPRN&gt;&lt;![CDATA[P02_100]]&gt;&lt;/SNDPRN&gt;
              &lt;RCVPOR&gt;INTERNET&lt;/RCVPOR&gt;
              &lt;RCVPRT&gt;LS&lt;/RCVPRT&gt;
              &lt;RCVPRN&gt;INTERNET&lt;/RCVPRN&gt;
              &lt;CREDAT&gt;20190503&lt;/CREDAT&gt;
              &lt;CRETIM&gt;085059&lt;/CRETIM&gt;
              &lt;SERIAL&gt;20190503081911&lt;/SERIAL&gt;
          &lt;/EDI_DC40&gt;
          &lt;E1KNA1M SEGMENT="1"&gt;
              &lt;MSGFN&gt;004&lt;/MSGFN&gt;
              &lt;KUNNR&gt;XXX&lt;/KUNNR&gt;
              &lt;ANRED&gt;XXX&lt;/ANRED&gt;
              &lt;BBBNR&gt;0000000&lt;/BBBNR&gt;
              &lt;BBSNR&gt;00000&lt;/BBSNR&gt;
              &lt;BUBKZ&gt;0&lt;/BUBKZ&gt;
              &lt;KTOKD&gt;XXX&lt;/KTOKD&gt;
              &lt;LAND1&gt;XX&lt;/LAND1&gt;
              &lt;NAME1&gt;XXX&lt;/NAME1&gt;
              &lt;ORT01&gt;XXX&lt;/ORT01&gt;
              &lt;ORT02&gt;XXX&lt;/ORT02&gt;
              &lt;PSTLZ&gt;XXX&lt;/PSTLZ&gt;
              &lt;REGIO&gt;XXX&lt;/REGIO&gt;
              &lt;SORTL&gt;XXX&lt;/SORTL&gt;
              &lt;SPRAS&gt;XXX&lt;/SPRAS&gt;
              &lt;STRAS&gt;XXX&lt;/STRAS&gt;
              &lt;LZONE&gt;0000000001&lt;/LZONE&gt;
              &lt;UMJAH&gt;0000&lt;/UMJAH&gt;
              &lt;JMZAH&gt;000000&lt;/JMZAH&gt;
              &lt;JMJAH&gt;0000&lt;/JMJAH&gt;
              &lt;KATR3&gt;XXX&lt;/KATR3&gt;
              &lt;KATR7&gt;XXX&lt;/KATR7&gt;
              &lt;KATR8&gt;XXX&lt;/KATR8&gt;
              &lt;STKZN&gt;X&lt;/STKZN&gt;
              &lt;UMSA1&gt;0&lt;/UMSA1&gt;
              &lt;HZUOR&gt;00&lt;/HZUOR&gt;
              &lt;CIVVE&gt;X&lt;/CIVVE&gt;
              &lt;SPRAS_ISO&gt;DE&lt;/SPRAS_ISO&gt;
              &lt;ZE1BPAD SEGMENT="1"&gt;
                  &lt;PARNR&gt;XXX&lt;/PARNR&gt;
                  &lt;TITLE_P&gt;XXX&lt;/TITLE_P&gt;
                  &lt;FIRSTNAME&gt;XXX&lt;/FIRSTNAME&gt;
                  &lt;LASTNAME&gt;XXX&lt;/LASTNAME&gt;
                  &lt;SORT1_P&gt;XXX&lt;/SORT1_P&gt;
                  &lt;NAMCOUNTRY&gt;DE&lt;/NAMCOUNTRY&gt;
                  &lt;NAMCTRYISO&gt;DE&lt;/NAMCTRYISO&gt;
                  &lt;LANGU_P&gt;D&lt;/LANGU_P&gt;
                  &lt;LANGUP_ISO&gt;DE&lt;/LANGUP_ISO&gt;
              &lt;C_O_NAME&gt;XXX&lt;/C_O_NAME&gt;
              &lt;DISTRICT&gt;XXX&lt;/DISTRICT&gt;
              &lt;CITY&gt;XXX&lt;/CITY&gt;
              &lt;POSTL_COD1&gt;XXX&lt;/POSTL_COD1&gt;
              &lt;REGION&gt;XXX&lt;/REGION&gt;
              &lt;TRANSPZONE&gt;0000000001&lt;/TRANSPZONE&gt;
              &lt;STREET&gt;XXX.&lt;/STREET&gt;
              &lt;HOUSE_NO&gt;XXX&lt;/HOUSE_NO&gt;
              &lt;PERS_GROUP&gt;XXX&lt;/PERS_GROUP&gt;
              &lt;ADDR_GROUP&gt;XXX&lt;/ADDR_GROUP&gt;
              &lt;E_MAIL&gt;XXX&lt;/E_MAIL&gt;
          &lt;/ZE1BPAD&gt;
          &lt;E1KNA11 SEGMENT="1"&gt;
              &lt;KNURL&gt;/&lt;/KNURL&gt;
              &lt;J_1KFREPRE&gt;/&lt;/J_1KFREPRE&gt;
              &lt;J_1KFTBUS&gt;/&lt;/J_1KFTBUS&gt;
              &lt;J_1KFTIND&gt;/&lt;/J_1KFTIND&gt;
              &lt;PSOIS&gt;/&lt;/PSOIS&gt;
              &lt;PSON1&gt;/&lt;/PSON1&gt;
              &lt;PSON2&gt;/&lt;/PSON2&gt;
              &lt;PSON3&gt;/&lt;/PSON3&gt;
              &lt;PSOVN&gt;/&lt;/PSOVN&gt;
              &lt;PSOTL&gt;/&lt;/PSOTL&gt;
              &lt;PSOO1&gt;/&lt;/PSOO1&gt;
              &lt;PSOO2&gt;/&lt;/PSOO2&gt;
              &lt;PSOO3&gt;/&lt;/PSOO3&gt;
              &lt;PSOO4&gt;/&lt;/PSOO4&gt;
              &lt;PSOO5&gt;/&lt;/PSOO5&gt;
              &lt;STCD5&gt;/&lt;/STCD5&gt;
              &lt;SUFRAMA&gt;/&lt;/SUFRAMA&gt;
              &lt;RG&gt;/&lt;/RG&gt;
              &lt;EXP&gt;/&lt;/EXP&gt;
              &lt;UF&gt;/&lt;/UF&gt;
              &lt;RGDATE&gt;/&lt;/RGDATE&gt;
              &lt;RIC&gt;/&lt;/RIC&gt;
              &lt;RNE&gt;/&lt;/RNE&gt;
              &lt;RNEDATE&gt;/&lt;/RNEDATE&gt;
              &lt;CNAE&gt;/&lt;/CNAE&gt;
              &lt;LEGALNAT&gt;/&lt;/LEGALNAT&gt;
              &lt;CRTN&gt;/&lt;/CRTN&gt;
              &lt;ICMSTAXPAY&gt;/&lt;/ICMSTAXPAY&gt;
              &lt;INDTYP&gt;/&lt;/INDTYP&gt;
              &lt;TDT&gt;/&lt;/TDT&gt;
              &lt;COMSIZE&gt;/&lt;/COMSIZE&gt;
              &lt;DECREGPC&gt;/&lt;/DECREGPC&gt;
              &lt;CVP_XBLCK&gt;/&lt;/CVP_XBLCK&gt;
          &lt;/E1KNA11&gt;
          &lt;E1KNVVM SEGMENT="1"&gt;
              &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
              &lt;VKORG&gt;XXX&lt;/VKORG&gt;
              &lt;VTWEG&gt;XXX&lt;/VTWEG&gt;
              &lt;SPART&gt;XXX&lt;/SPART&gt;
              &lt;VERSG&gt;1&lt;/VERSG&gt;
              &lt;KALKS&gt;XXX&lt;/KALKS&gt;
              &lt;KONDA&gt;XXX&lt;/KONDA&gt;
              &lt;AWAHR&gt;XXX&lt;/AWAHR&gt;
              &lt;INCO1&gt;FH&lt;/INCO1&gt;
              &lt;INCO2&gt;XXX&lt;/INCO2&gt;
              &lt;ANTLF&gt;XXX&lt;/ANTLF&gt;
              &lt;KZTLF&gt;XXX&lt;/KZTLF&gt;
              &lt;LPRIO&gt;XXX&lt;/LPRIO&gt;
              &lt;WAERS&gt;XXX&lt;/WAERS&gt;
              &lt;KTGRD&gt;XXX&lt;/KTGRD&gt;
              &lt;ZTERM&gt;XXX&lt;/ZTERM&gt;
              &lt;VWERK&gt;XXX&lt;/VWERK&gt;
              &lt;KVGR4&gt;XXX&lt;/KVGR4&gt;
              &lt;KVGR5&gt;XXX&lt;/KVGR5&gt;
              &lt;UEBTO&gt;XXX&lt;/UEBTO&gt;
              &lt;UNTTO&gt;XXX&lt;/UNTTO&gt;
              &lt;PVKSM&gt;XXX&lt;/PVKSM&gt;
              &lt;PODTG&gt;XXX&lt;/PODTG&gt;
              &lt;BLIND&gt;/&lt;/BLIND&gt;
              &lt;CARRIER_NOTIF&gt;/&lt;/CARRIER_NOTIF&gt;
              &lt;CVP_XBLCK_V&gt;/&lt;/CVP_XBLCK_V&gt;
              &lt;INCOV&gt;/&lt;/INCOV&gt;
              &lt;INCO2_L&gt;/&lt;/INCO2_L&gt;
              &lt;INCO3_L&gt;/&lt;/INCO3_L&gt;
              &lt;ZE1KNVV SEGMENT="1"&gt;
                  &lt;HASH_CODE&gt;XXX&lt;/HASH_CODE&gt;
              &lt;/ZE1KNVV&gt;
              &lt;E1KNVPM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
                  &lt;PARVW&gt;XXX&lt;/PARVW&gt;
                  &lt;KUNN2&gt;XXX&lt;/KUNN2&gt;
                  &lt;PARZA&gt;XXX&lt;/PARZA&gt;
              &lt;/E1KNVPM&gt;
              &lt;E1KNVPM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
                  &lt;PARVW&gt;XXX&lt;/PARVW&gt;
                  &lt;KUNN2&gt;XXX&lt;/KUNN2&gt;
                  &lt;PARZA&gt;XXX&lt;/PARZA&gt;
              &lt;/E1KNVPM&gt;
              &lt;E1KNVPM SEGMENT="1"&gt;
                  &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
                  &lt;PARVW&gt;WE&lt;/PARVW&gt;
                  &lt;KUNN2&gt;XXX&lt;/KUNN2&gt;
                  &lt;PARZA&gt;XXX&lt;/PARZA&gt;
              &lt;/E1KNVPM&gt;
          &lt;/E1KNVVM&gt;
          &lt;E1KNB1M SEGMENT="1"&gt;
              &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
              &lt;BUKRS&gt;XXX&lt;/BUKRS&gt;
              &lt;ZUAWA&gt;XXX&lt;/ZUAWA&gt;
              &lt;AKONT&gt;XXX&lt;/AKONT&gt;
              &lt;ZWELS&gt;XXX&lt;/ZWELS&gt;
              &lt;ZTERM&gt;XXX&lt;/ZTERM&gt;
              &lt;VZSKZ&gt;XXX&lt;/VZSKZ&gt;
              &lt;ZINDT&gt;XXX&lt;/ZINDT&gt;
              &lt;ZINRT&gt;XXX&lt;/ZINRT&gt;
              &lt;FDGRV&gt;XXX&lt;/FDGRV&gt;
              &lt;VLIBB&gt;XXX&lt;/VLIBB&gt;
              &lt;VRSZL&gt;XXX&lt;/VRSZL&gt;
              &lt;VRSPR&gt;XXX&lt;/VRSPR&gt;
              &lt;VERDT&gt;XXX&lt;/VERDT&gt;
              &lt;WEBTR&gt;XXX&lt;/WEBTR&gt;
              &lt;DATLZ&gt;XXX&lt;/DATLZ&gt;
              &lt;XZVER&gt;XXX&lt;/XZVER&gt;
              &lt;KULTG&gt;XXX&lt;/KULTG&gt;
              &lt;PERNR&gt;XXX&lt;/PERNR&gt;
              &lt;GMVKZD&gt;/&lt;/GMVKZD&gt;
              &lt;AVSND&gt;/&lt;/AVSND&gt;
              &lt;SMTP_ADDR&gt;/&lt;/SMTP_ADDR&gt;
              &lt;CVP_XBLCK_B&gt;/&lt;/CVP_XBLCK_B&gt;
              &lt;E1KNB5M SEGMENT="1"&gt;
                  &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
                  &lt;MAHNA&gt;XXX&lt;/MAHNA&gt;
                  &lt;MADAT&gt;XXX&lt;/MADAT&gt;
                  &lt;MAHNS&gt;XXX&lt;/MAHNS&gt;
                  &lt;GMVDT&gt;XXX&lt;/GMVDT&gt;
              &lt;/E1KNB5M&gt;
              &lt;E1KNB5M SEGMENT="1"&gt;
                  &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
                  &lt;MABER&gt;XXX&lt;/MABER&gt;
                  &lt;MAHNA&gt;XXX&lt;/MAHNA&gt;
                  &lt;MADAT&gt;XXX&lt;/MADAT&gt;
                  &lt;MAHNS&gt;XXX&lt;/MAHNS&gt;
                  &lt;GMVDT&gt;XXX&lt;/GMVDT&gt;
              &lt;/E1KNB5M&gt;
          &lt;/E1KNB1M&gt;
          &lt;E1KNKKM SEGMENT="1"&gt;
              &lt;MSGFN&gt;XXX&lt;/MSGFN&gt;
              &lt;KKBER&gt;XXX&lt;/KKBER&gt;
              &lt;KLIMK&gt;XXX&lt;/KLIMK&gt;
              &lt;KNKLI&gt;XXX&lt;/KNKLI&gt;
              &lt;CTLPC&gt;XXX&lt;/CTLPC&gt;
              &lt;DTREV&gt;XXX&lt;/DTREV&gt;
              &lt;SBGRP&gt;XXX&lt;/SBGRP&gt;
              &lt;NXTRV&gt;XXX&lt;/NXTRV&gt;
              &lt;PAYDB&gt;XXX&lt;/PAYDB&gt;
              &lt;REVDB&gt;XXX&lt;/REVDB&gt;
              &lt;SBDAT&gt;XXX&lt;/SBDAT&gt;
              &lt;DBEKR&gt;XXX&lt;/DBEKR&gt;
              &lt;DBMON&gt;XXX&lt;/DBMON&gt;
          &lt;/E1KNKKM&gt;
      &lt;/E1KNA1M&gt;
      &lt;EDI_DS40 SEGMENT="1"&gt;
          &lt;MANDT&gt;XXX&lt;/MANDT&gt;
          &lt;DOCNUM&gt;XXX&lt;/DOCNUM&gt;
          &lt;LOGDAT&gt;XXX&lt;/LOGDAT&gt;
          &lt;LOGTIM&gt;XXX&lt;/LOGTIM&gt;
          &lt;STATUS&gt;XXX&lt;/STATUS&gt;
          &lt;STAMNO&gt;XXX&lt;/STAMNO&gt;
          &lt;UNAME&gt;&lt;![CDATA[BTCSD_01]]&gt;&lt;/UNAME&gt;
          &lt;SEGNUM&gt;XXX&lt;/SEGNUM&gt;
      &lt;/EDI_DS40&gt;
      &lt;EDI_DS40 SEGMENT="1"&gt;
          &lt;MANDT&gt;XXX&lt;/MANDT&gt;
          &lt;DOCNUM&gt;XXX&lt;/DOCNUM&gt;
          &lt;LOGDAT&gt;XXX&lt;/LOGDAT&gt;
          &lt;LOGTIM&gt;XXX&lt;/LOGTIM&gt;
          &lt;STATUS&gt;XXX&lt;/STATUS&gt;
          &lt;STAMQU&gt;XXX&lt;/STAMQU&gt;
          &lt;STAMID&gt;XXX&lt;/STAMID&gt;
          &lt;STAMNO&gt;XXX&lt;/STAMNO&gt;
          &lt;STATYP&gt;XXX&lt;/STATYP&gt;
          &lt;STAPA1&gt;XXX&lt;/STAPA1&gt;
          &lt;STAPA2&gt;XXX&lt;/STAPA2&gt;
          &lt;STAPA3&gt;XXX&lt;/STAPA3&gt;
          &lt;STAPA4&gt;XXX&lt;/STAPA4&gt;
          &lt;STATXT&gt;&lt;![CDATA[&amp;, &amp;, &amp;, &amp;.]]&gt;&lt;/STATXT&gt;
          &lt;UNAME&gt;&lt;![CDATA[BTCSD_01]]&gt;&lt;/UNAME&gt;
          &lt;SEGNUM&gt;XXX&lt;/SEGNUM&gt;
      &lt;/EDI_DS40&gt;
      &lt;EDI_DS40 SEGMENT="1"&gt;
          &lt;MANDT&gt;XXX&lt;/MANDT&gt;
          &lt;DOCNUM&gt;XXX&lt;/DOCNUM&gt;
          &lt;LOGDAT&gt;XXX&lt;/LOGDAT&gt;
          &lt;LOGTIM&gt;XXX&lt;/LOGTIM&gt;
          &lt;STATUS&gt;XXX&lt;/STATUS&gt;
          &lt;STAMQU&gt;XXX&lt;/STAMQU&gt;
          &lt;STAMID&gt;XXX&lt;/STAMID&gt;
          &lt;STAMNO&gt;XXX&lt;/STAMNO&gt;
          &lt;STAPA1&gt;&lt;![CDATA[SAP_ALE_Z_DEBMAS2]]&gt;&lt;/STAPA1&gt;
          &lt;STAPA2&gt;XXX&lt;/STAPA2&gt;
          &lt;STATXT&gt;XXX&lt;/STATXT&gt;
          &lt;UNAME&gt;&lt;![CDATA[BTCSD_01]]&gt;&lt;/UNAME&gt;
          &lt;REPID&gt;XXX&lt;/REPID&gt;
          &lt;SEGNUM&gt;XXX&lt;/SEGNUM&gt;
      &lt;/EDI_DS40&gt;
      &lt;EDI_DS40 SEGMENT="1"&gt;
          &lt;MANDT&gt;XXX&lt;/MANDT&gt;
          &lt;DOCNUM&gt;XXX&lt;/DOCNUM&gt;
          &lt;LOGDAT&gt;XXX&lt;/LOGDAT&gt;
          &lt;LOGTIM&gt;XXX&lt;/LOGTIM&gt;
          &lt;STATUS&gt;XXX&lt;/STATUS&gt;
          &lt;STAMNO&gt;XXX&lt;/STAMNO&gt;
          &lt;UNAME&gt;&lt;![CDATA[BTCSD_01]]&gt;&lt;/UNAME&gt;
          &lt;REPID&gt;RBDMOIND&lt;/REPID&gt;
          &lt;SEGNUM&gt;XXX&lt;/SEGNUM&gt;
      &lt;/EDI_DS40&gt;
  &lt;/IDOC&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Update 2</strong></p>
  
  <p>Just for testing I tried to create an IDOC manually with the following piece of code</p>
  
  <pre><code>    JCoIDocServer server = JCoIDoc.getServer(sapProperties.get("jco.server.progid"))
      IDocDocument idoc = JCoIDoc.getIDocFactory().createIDocDocument(server.getIDocRepository(), "DEBMAS05")
      idoc.setValue("MANDT", 100)
      idoc.setValue("IDOCTYP", "DEBMAS05")
      idoc.setValue("CIMTYP", "ZDEBMAS3")
      idoc.setIDocNumber("0000000406868135")
  
  
      JCoIDocSegment segment = idoc.rootSegment.addChild("E1KNA1M")
  
      segment.addChild("ZE1BPAD")
  </code></pre>
  
  <p>This leads to the same error as using the xmlProcessor.</p>
  
  <p>But if I change this loc adding the IDOC extension type</p>
  
  <pre><code>IDocDocument idoc = JCoIDoc.getIDocFactory().createIDocDocument(server.getIDocRepository(), "DEBMAS05","ZDEBMAS3")
  </code></pre>
  
  <p>the error disappears.</p>
  
  <p><strong>Update 3</strong></p>
  
  <p>I seem to mix up two problems. The first occurs while JCo parses the XML causing the exception described here.</p>
  
  <p>In JCo 3.0.17 the causing implementation is in DefaultIDocXMLParser in line 1321</p>
  
  <pre><code>   if (cimType.length() &gt; 0 &amp;&amp; !cimType.equals(this.openTags.getFirst()) || cimType.length() == 0 &amp;&amp; !iDocType.equals(this.openTags.getFirst())) {
                              throw new IDocParseException("IDoc type " + (cimType.length() &gt; 0 ? "extension " + cimType : iDocType) + " within the EDI_DC40 control record segment does not match the IDoc-XML root tag &lt;" + (String)this.openTags.getFirst() + "&gt;", this.state.name(), this.charPosition, this.lineNumber, this.columnNumber, (String)null, 0);
                          }
  </code></pre>
  
  <p>To avoid this exception one can remove the  element which causes the same exception as the programmatically created IDOC without the extension type.</p>
  
  <p>The question is now: How can the XMLParser be configured to accept extensions?</p>