SAP JCo使用字段扩展将XML解析为IDOC

时间:2019-05-14 07:58:11

标签: xml sap jco idoc

我在使用SAP JCo库将XML解析为IDOC时遇到一些问题。我知道我需要连接到给出的SAP系统。

这是我的gradle测试代码:

    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)

该连接有效,但是JCo发现我的XML(从SAP导出,没有人工更改!)不一致,这似乎是由SAP扩展字段引起的:

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 <DEBMAS05>:

state = READING_ENDTAG,charPosition = 852,lineNumber = 24,columnNumber = 19

如您所见,我尝试使用XML解析器选项,但没有帮助。

IDocParser接受扩展字段该怎么办?

更新1 在这里,您可以找到匿名的IDOC XML。

<?xml version="1.0"?>
<DEBMAS05>
    <IDOC BEGIN="1">
        <EDI_DC40 SEGMENT="1">
            <TABNAM><![CDATA[EDI_DC40]]></TABNAM>
            <MANDT>100</MANDT>
            <DOCNUM>0000000406868135</DOCNUM>
            <DOCREL>750</DOCREL>
            <STATUS>12</STATUS>
            <DIRECT>1</DIRECT>
            <OUTMOD>2</OUTMOD>
            <IDOCTYP>DEBMAS05</IDOCTYP>
            <CIMTYP>ZDEBMAS3</CIMTYP>
            <MESTYP><![CDATA[Z_DEBMAS2]]></MESTYP>
            <SNDPOR>XXX</SNDPOR>
            <SNDPRT>LS</SNDPRT>
            <SNDPRN><![CDATA[P02_100]]></SNDPRN>
            <RCVPOR>INTERNET</RCVPOR>
            <RCVPRT>LS</RCVPRT>
            <RCVPRN>INTERNET</RCVPRN>
            <CREDAT>20190503</CREDAT>
            <CRETIM>085059</CRETIM>
            <SERIAL>20190503081911</SERIAL>
        </EDI_DC40>
        <E1KNA1M SEGMENT="1">
            <MSGFN>004</MSGFN>
            <KUNNR>XXX</KUNNR>
            <ANRED>XXX</ANRED>
            <BBBNR>0000000</BBBNR>
            <BBSNR>00000</BBSNR>
            <BUBKZ>0</BUBKZ>
            <KTOKD>XXX</KTOKD>
            <LAND1>XX</LAND1>
            <NAME1>XXX</NAME1>
            <ORT01>XXX</ORT01>
            <ORT02>XXX</ORT02>
            <PSTLZ>XXX</PSTLZ>
            <REGIO>XXX</REGIO>
            <SORTL>XXX</SORTL>
            <SPRAS>XXX</SPRAS>
            <STRAS>XXX</STRAS>
            <LZONE>0000000001</LZONE>
            <UMJAH>0000</UMJAH>
            <JMZAH>000000</JMZAH>
            <JMJAH>0000</JMJAH>
            <KATR3>XXX</KATR3>
            <KATR7>XXX</KATR7>
            <KATR8>XXX</KATR8>
            <STKZN>X</STKZN>
            <UMSA1>0</UMSA1>
            <HZUOR>00</HZUOR>
            <CIVVE>X</CIVVE>
            <SPRAS_ISO>DE</SPRAS_ISO>
            <ZE1BPAD SEGMENT="1">
                <PARNR>XXX</PARNR>
                <TITLE_P>XXX</TITLE_P>
                <FIRSTNAME>XXX</FIRSTNAME>
                <LASTNAME>XXX</LASTNAME>
                <SORT1_P>XXX</SORT1_P>
                <NAMCOUNTRY>DE</NAMCOUNTRY>
                <NAMCTRYISO>DE</NAMCTRYISO>
                <LANGU_P>D</LANGU_P>
                <LANGUP_ISO>DE</LANGUP_ISO>
            <C_O_NAME>XXX</C_O_NAME>
            <DISTRICT>XXX</DISTRICT>
            <CITY>XXX</CITY>
            <POSTL_COD1>XXX</POSTL_COD1>
            <REGION>XXX</REGION>
            <TRANSPZONE>0000000001</TRANSPZONE>
            <STREET>XXX.</STREET>
            <HOUSE_NO>XXX</HOUSE_NO>
            <PERS_GROUP>XXX</PERS_GROUP>
            <ADDR_GROUP>XXX</ADDR_GROUP>
            <E_MAIL>XXX</E_MAIL>
        </ZE1BPAD>
        <E1KNA11 SEGMENT="1">
            <KNURL>/</KNURL>
            <J_1KFREPRE>/</J_1KFREPRE>
            <J_1KFTBUS>/</J_1KFTBUS>
            <J_1KFTIND>/</J_1KFTIND>
            <PSOIS>/</PSOIS>
            <PSON1>/</PSON1>
            <PSON2>/</PSON2>
            <PSON3>/</PSON3>
            <PSOVN>/</PSOVN>
            <PSOTL>/</PSOTL>
            <PSOO1>/</PSOO1>
            <PSOO2>/</PSOO2>
            <PSOO3>/</PSOO3>
            <PSOO4>/</PSOO4>
            <PSOO5>/</PSOO5>
            <STCD5>/</STCD5>
            <SUFRAMA>/</SUFRAMA>
            <RG>/</RG>
            <EXP>/</EXP>
            <UF>/</UF>
            <RGDATE>/</RGDATE>
            <RIC>/</RIC>
            <RNE>/</RNE>
            <RNEDATE>/</RNEDATE>
            <CNAE>/</CNAE>
            <LEGALNAT>/</LEGALNAT>
            <CRTN>/</CRTN>
            <ICMSTAXPAY>/</ICMSTAXPAY>
            <INDTYP>/</INDTYP>
            <TDT>/</TDT>
            <COMSIZE>/</COMSIZE>
            <DECREGPC>/</DECREGPC>
            <CVP_XBLCK>/</CVP_XBLCK>
        </E1KNA11>
        <E1KNVVM SEGMENT="1">
            <MSGFN>XXX</MSGFN>
            <VKORG>XXX</VKORG>
            <VTWEG>XXX</VTWEG>
            <SPART>XXX</SPART>
            <VERSG>1</VERSG>
            <KALKS>XXX</KALKS>
            <KONDA>XXX</KONDA>
            <AWAHR>XXX</AWAHR>
            <INCO1>FH</INCO1>
            <INCO2>XXX</INCO2>
            <ANTLF>XXX</ANTLF>
            <KZTLF>XXX</KZTLF>
            <LPRIO>XXX</LPRIO>
            <WAERS>XXX</WAERS>
            <KTGRD>XXX</KTGRD>
            <ZTERM>XXX</ZTERM>
            <VWERK>XXX</VWERK>
            <KVGR4>XXX</KVGR4>
            <KVGR5>XXX</KVGR5>
            <UEBTO>XXX</UEBTO>
            <UNTTO>XXX</UNTTO>
            <PVKSM>XXX</PVKSM>
            <PODTG>XXX</PODTG>
            <BLIND>/</BLIND>
            <CARRIER_NOTIF>/</CARRIER_NOTIF>
            <CVP_XBLCK_V>/</CVP_XBLCK_V>
            <INCOV>/</INCOV>
            <INCO2_L>/</INCO2_L>
            <INCO3_L>/</INCO3_L>
            <ZE1KNVV SEGMENT="1">
                <HASH_CODE>XXX</HASH_CODE>
            </ZE1KNVV>
            <E1KNVPM SEGMENT="1">
                <MSGFN>XXX</MSGFN>
                <PARVW>XXX</PARVW>
                <KUNN2>XXX</KUNN2>
                <PARZA>XXX</PARZA>
            </E1KNVPM>
            <E1KNVPM SEGMENT="1">
                <MSGFN>XXX</MSGFN>
                <PARVW>XXX</PARVW>
                <KUNN2>XXX</KUNN2>
                <PARZA>XXX</PARZA>
            </E1KNVPM>
            <E1KNVPM SEGMENT="1">
                <MSGFN>XXX</MSGFN>
                <PARVW>WE</PARVW>
                <KUNN2>XXX</KUNN2>
                <PARZA>XXX</PARZA>
            </E1KNVPM>
        </E1KNVVM>
        <E1KNB1M SEGMENT="1">
            <MSGFN>XXX</MSGFN>
            <BUKRS>XXX</BUKRS>
            <ZUAWA>XXX</ZUAWA>
            <AKONT>XXX</AKONT>
            <ZWELS>XXX</ZWELS>
            <ZTERM>XXX</ZTERM>
            <VZSKZ>XXX</VZSKZ>
            <ZINDT>XXX</ZINDT>
            <ZINRT>XXX</ZINRT>
            <FDGRV>XXX</FDGRV>
            <VLIBB>XXX</VLIBB>
            <VRSZL>XXX</VRSZL>
            <VRSPR>XXX</VRSPR>
            <VERDT>XXX</VERDT>
            <WEBTR>XXX</WEBTR>
            <DATLZ>XXX</DATLZ>
            <XZVER>XXX</XZVER>
            <KULTG>XXX</KULTG>
            <PERNR>XXX</PERNR>
            <GMVKZD>/</GMVKZD>
            <AVSND>/</AVSND>
            <SMTP_ADDR>/</SMTP_ADDR>
            <CVP_XBLCK_B>/</CVP_XBLCK_B>
            <E1KNB5M SEGMENT="1">
                <MSGFN>XXX</MSGFN>
                <MAHNA>XXX</MAHNA>
                <MADAT>XXX</MADAT>
                <MAHNS>XXX</MAHNS>
                <GMVDT>XXX</GMVDT>
            </E1KNB5M>
            <E1KNB5M SEGMENT="1">
                <MSGFN>XXX</MSGFN>
                <MABER>XXX</MABER>
                <MAHNA>XXX</MAHNA>
                <MADAT>XXX</MADAT>
                <MAHNS>XXX</MAHNS>
                <GMVDT>XXX</GMVDT>
            </E1KNB5M>
        </E1KNB1M>
        <E1KNKKM SEGMENT="1">
            <MSGFN>XXX</MSGFN>
            <KKBER>XXX</KKBER>
            <KLIMK>XXX</KLIMK>
            <KNKLI>XXX</KNKLI>
            <CTLPC>XXX</CTLPC>
            <DTREV>XXX</DTREV>
            <SBGRP>XXX</SBGRP>
            <NXTRV>XXX</NXTRV>
            <PAYDB>XXX</PAYDB>
            <REVDB>XXX</REVDB>
            <SBDAT>XXX</SBDAT>
            <DBEKR>XXX</DBEKR>
            <DBMON>XXX</DBMON>
        </E1KNKKM>
    </E1KNA1M>
    <EDI_DS40 SEGMENT="1">
        <MANDT>XXX</MANDT>
        <DOCNUM>XXX</DOCNUM>
        <LOGDAT>XXX</LOGDAT>
        <LOGTIM>XXX</LOGTIM>
        <STATUS>XXX</STATUS>
        <STAMNO>XXX</STAMNO>
        <UNAME><![CDATA[BTCSD_01]]></UNAME>
        <SEGNUM>XXX</SEGNUM>
    </EDI_DS40>
    <EDI_DS40 SEGMENT="1">
        <MANDT>XXX</MANDT>
        <DOCNUM>XXX</DOCNUM>
        <LOGDAT>XXX</LOGDAT>
        <LOGTIM>XXX</LOGTIM>
        <STATUS>XXX</STATUS>
        <STAMQU>XXX</STAMQU>
        <STAMID>XXX</STAMID>
        <STAMNO>XXX</STAMNO>
        <STATYP>XXX</STATYP>
        <STAPA1>XXX</STAPA1>
        <STAPA2>XXX</STAPA2>
        <STAPA3>XXX</STAPA3>
        <STAPA4>XXX</STAPA4>
        <STATXT><![CDATA[&, &, &, &.]]></STATXT>
        <UNAME><![CDATA[BTCSD_01]]></UNAME>
        <SEGNUM>XXX</SEGNUM>
    </EDI_DS40>
    <EDI_DS40 SEGMENT="1">
        <MANDT>XXX</MANDT>
        <DOCNUM>XXX</DOCNUM>
        <LOGDAT>XXX</LOGDAT>
        <LOGTIM>XXX</LOGTIM>
        <STATUS>XXX</STATUS>
        <STAMQU>XXX</STAMQU>
        <STAMID>XXX</STAMID>
        <STAMNO>XXX</STAMNO>
        <STAPA1><![CDATA[SAP_ALE_Z_DEBMAS2]]></STAPA1>
        <STAPA2>XXX</STAPA2>
        <STATXT>XXX</STATXT>
        <UNAME><![CDATA[BTCSD_01]]></UNAME>
        <REPID>XXX</REPID>
        <SEGNUM>XXX</SEGNUM>
    </EDI_DS40>
    <EDI_DS40 SEGMENT="1">
        <MANDT>XXX</MANDT>
        <DOCNUM>XXX</DOCNUM>
        <LOGDAT>XXX</LOGDAT>
        <LOGTIM>XXX</LOGTIM>
        <STATUS>XXX</STATUS>
        <STAMNO>XXX</STAMNO>
        <UNAME><![CDATA[BTCSD_01]]></UNAME>
        <REPID>RBDMOIND</REPID>
        <SEGNUM>XXX</SEGNUM>
    </EDI_DS40>
</IDOC>

更新2

仅出于测试目的,我尝试使用以下代码手动创建IDOC

    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")

这导致与使用xmlProcessor相同的错误。

但是如果我更改此位置并添加IDOC扩展类型

IDocDocument idoc = JCoIDoc.getIDocFactory().createIDocDocument(server.getIDocRepository(), "DEBMAS05","ZDEBMAS3")

错误消失。

更新3

我似乎混淆了两个问题。第一次发生在JCo解析XML导致此处描述的异常时。

在JCo 3.0.17中,引起原因的实现在第1321行的DefaultIDocXMLParser中

   if (cimType.length() > 0 && !cimType.equals(this.openTags.getFirst()) || cimType.length() == 0 && !iDocType.equals(this.openTags.getFirst())) {
                            throw new IDocParseException("IDoc type " + (cimType.length() > 0 ? "extension " + cimType : iDocType) + " within the EDI_DC40 control record segment does not match the IDoc-XML root tag <" + (String)this.openTags.getFirst() + ">", this.state.name(), this.charPosition, this.lineNumber, this.columnNumber, (String)null, 0);
                        }

为避免此异常,可以删除导致与以编程方式创建的IDOC相同的异常(不带扩展名)的元素。

现在的问题是:如何将XMLParser配置为接受扩展?

2 个答案:

答案 0 :(得分:0)

您需要传递有效的阅读器或输入流。 XML文件不是类的资源。 所以,请您尝试

new FileInputStream(new File("/data/DEBMAS-406868135.XML"))

相反?

答案 1 :(得分:0)

现在,在添加IDoc-XML内容之后,我会说解析器的错误消息确实是正确的。 IDoc-XML文件内容违反了SAP的IDoc-XML规范。 在这种情况下,根标记必须是而不是

根标记必须描述最详细的IDoc类型值,这意味着只要不再有定义CIMTYP可用,它就必须包含IDOCTYP字段值。但是,一旦有其他CIMTYP,则根标记必须改为包含CIMTYP字段值。这是一个奇怪的规则,但这是规则。

因此IDoc-XML解析器不是错误的,而是产生IDoc-XML文件的解析器。 恐怕此IDoc-XML解析器没有选项来接受此错误的IDoc-XML数据。您需要纠正产生此IDoc-XML文件的软件。