1 Reply Latest reply on May 30, 2016 6:27 AM by 2675835

    Unable to read multi-level nodes in xml file.

    2675835

      Hello All,
      Hope you are doing well,

      Question :
      I need to read an external xml file in JDE and populate the custom table.
      Sample is the xml which i am trying to read using XERCES parser.

      Issue :
      I am able to get the values which are at level 1. ie. <retConsReciNFe>
      But when I try to access the details of tag <infProt>, i get an issue. It does'nt brings the right value.
      Instead it gives me Bad Pointer.For Accessing the elements i am using JDE Standard API's.
      Highlighted as bold & Red in the code.

      My Comments :
      1) Do we have any better approach.
      2) I refered BSFN B76B527 for reading xml.

      ------------------------------
      Sample XML File.
      ------------------------------
      <retConsReciNFe>
      <cStat>104</cStat>
      <protNFe>test1</protNFe>
      <infProt>
      <chNFe>Test1</chNFe>
      <nProt>Test2</nProt>
      <cStat>Alfa</cStat>
      <dhRecbto>USA</dhRecbto>
      </infProt>
      <infProt>
      <chNFe>Test1</chNFe>
      <nProt>Test2</nProt>
      <cStat>Alfa</cStat>
      <dhRecbto>india</dhRecbto>
      </infProt>
      </retConsReciNFe>

      ------------------------------
      C Business Function Code.
      ------------------------------

      #include <jde.h>

      #define b59xmlin_c

      #include <b59xmlin.h>
      JDEBFRTN (ID) JDEBFWINAPI ReadXMLFile (LPBHVRCOM lpBhvrCom, LPVOID lpVoid, LPDSD59XMLINBO lpDS)

      {
      /************************************************************************
      * Variable declarations
      ************************************************************************/
      HUSER hUser = (HUSER) NULL;
      ID idReturn = ER_SUCCESS;
      ID idReturn2 = ER_SUCCESS;
      ID idJDEDBReturn = JDEDB_PASSED;
      XRCS_Status xrcsStatus = XRCS_SUCCESS;
      XRCS_hParser hParser = NULL;
      XRCS_hDocument hDoc = (XRCS_hDocument) NULL;
      XRCS_hElement hRootElement = (XRCS_hElement) NULL;
      JCHAR *szRoot = NULL;
      BOOLEAN bProcessed = FALSE;
      JCHAR *szcStat = { 0 };
      XRCS_hElement* lph_protNFe = (XRCS_hElement*)NULL;
      XRCS_hElement* lph_infProt = (XRCS_hElement*)NULL;
      XRCS_hElement* lph_recBto = (XRCS_hElement*)NULL;
      XRCS_hElement* lph_chNFe = (XRCS_hElement*)NULL;
      XRCS_hElement* lph_szcStat = (XRCS_hElement*)NULL;
      XRCS_hElement h_childOfinfProt = (XRCS_hElement)NULL;
      unsigned int n_protNFe = 0;
      unsigned int n_childCount = 0;
      unsigned int nErrorCount = 0;
      JCHAR *elmValue = { 0 };
      /************************************************************************
      * Declare structures
      ************************************************************************/
      DSD76B527A_UpdNormal stUpdNormal;
      /************************************************************************
      * Declare pointers
      ************************************************************************/
      /************************************************************************
      * Check for NULL pointers
      ************************************************************************/
      if ((lpBhvrCom == (LPBHVRCOM) NULL) ||
      (lpVoid == (LPVOID) NULL) ||
      (lpDS == (LPDSD59XMLINBO) NULL))
      {
      jdeErrorSet (lpBhvrCom, lpVoid, (ID) 0, _J("4363"), (LPVOID) NULL);
      return ER_ERROR;
      }

      /************************************************************************
      * Set pointers
      ************************************************************************/

      /************************************************************************
      * Main Processing
      ************************************************************************/

      if (!IsStringBlank(lpDS->szXMLFilePath))
      {
      /* Initialize XML xerces engine */
      xrcsStatus = XRCS_initEngine();


      if(xrcsStatus != XRCS_SUCCESS)
      {
      idReturn = ER_ERROR;
      jdeVWriteLogEntry(_J("B59XMLIN"), __FILE__, __LINE__, 0, _J("Failed to initialize XML Parser"));
      }
      else
      {
      /* Get XML parser */
      xrcsStatus = XRCS_getParser(&hParser);
      if(xrcsStatus != XRCS_SUCCESS)
      {
      idReturn = ER_ERROR;
      jdeVWriteLogEntry(_J("B59XMLIN"), __FILE__, __LINE__, 0, _J("Failed to get XML Parser"));
      }
      else
      {
      /* Parse XML file */
      xrcsStatus = XRCS_parseXMLFile(hParser,
      lpDS->szXMLFilePath,
      &hDoc);


      if(xrcsStatus != XRCS_SUCCESS)
      {
      idReturn = ER_ERROR;
      jdeVWriteLogEntry(_J("B59XMLIN"), __FILE__, __LINE__, 0, _J("Failed to parse XML file"));
      }
      else
      {
      /* Navigate to the Root element */
      xrcsStatus = XRCS_getDocumentElement(hDoc, &hRootElement);

      if(xrcsStatus != XRCS_SUCCESS)
      {
      idReturn = ER_ERROR;
      jdeVWriteLogEntry(_J("B59XMLIN"), __FILE__, __LINE__, 0, _J("Failed to get Root Element"));
      }
      else
      {
      /* Get the Root element name */
      xrcsStatus = XRCS_getElementName(hRootElement, &szRoot);
      if( xrcsStatus != XRCS_SUCCESS )
      {
      idReturn = ER_ERROR;
      jdeVWriteLogEntry(_J("B59XMLIN"), __FILE__, __LINE__, 0, _J("Failed to get Root Element name"));
      }
      else
      {
      //If the root element is retConsReciNFe then process ahead. // this can be used when we have different files with different root element.
      if (jdeStrcmp(szRoot, _J("retConsReciNFe")) == 0)
      {
      xrcsStatus = XRCS_getElementsByTagName(hRootElement, _J("cStat"), &lph_szcStat, &nErrorCount);

      XRCS_getElementText(lph_szcStat[0],&elmValue);
      //XRCS_getElementsByTagName(hRootElement, XML_TAG_ERROR_CODE, &hElementErrCode, &nErrorCount)
      if( xrcsStatus != XRCS_SUCCESS)
      {
      idReturn = ER_ERROR;
      jdeVWriteLogEntry(_J("B59XMLIN"), __FILE__, __LINE__, 0, _J("Failed to get <cStat> element or value"));
      }
      else
      {
      xrcsStatus = XRCS_getElementsByTagName(hRootElement, _J("protNFe"), &lph_protNFe, &n_protNFe);
      if( xrcsStatus != XRCS_SUCCESS)
      {
      idReturn = ER_ERROR;
      jdeVWriteLogEntry(_J("B59XMLIN"), __FILE__, __LINE__, 0, _J("Failed to get <protNFe> element or value"));
      }
      else
      {
      unsigned int count = 0;
      XRCS_hElement* phColumnElm = NULL;
      unsigned int nColumnElm = 0;
      JCHAR* ddid = NULL;
      XRCS_hAttribute hAttr = NULL;


      xrcsStatus= XRCS_getElementsByTagName(hRootElement, _J("infProt"),&lph_infProt,&n_childCount);
      for(count=0;count<n_childCount;count++)
      {
      xrcsStatus= XRCS_getElementFirstChild(lph_infProt[count],&h_childOfinfProt);

      xrcsStatus =XRCS_getElementsByTagName(h_childOfinfProt, _J("dhRecbto"),&lph_recBto, &n_dhRecbto);

      xrcsStatus =XRCS_getElementText(lph_recBto[0],&elmValue);

      }

      }

       

      }
      }
      }
      }
      }
      }
      }
      }

       

      }