JD Edwards

JD Edwards
Lokesh Reddy Blog

Tuesday, June 14, 2016

Read XML Data & Copy into table


/*****************************************************************************
 *    Header File:  B5601XML.h
 *
 *    Description:  Read XML Data & Copy into table Header File
 *
 *        History:
 *          Date       Programmer  Email
 *          ----------  ----------  -------------------------------------------
 *   Author 11/10/2015  Lokesh Reddy       - Created
 *
 *
 * Copyright (c) J.D. Edwards World Source Company, 1996
 *
 * This unpublished material is proprietary to J.D. Edwards World Source
 * Company.  All rights reserved.  The methods and techniques described
 * herein are considered trade secrets and/or confidential.  Reproduction
 * or distribution, in whole or in part, is forbidden except by express
 * written permission of J.D. Edwards World Source Company.
 ****************************************************************************/

#ifndef __B5601XML_H
#define __B5601XML_H

/*****************************************************************************
 * Table Header Inclusions
 ****************************************************************************/

/*****************************************************************************
 * External Business Function Header Inclusions
 ****************************************************************************/
 #include <xerceswrapper.h>
/*****************************************************************************
 * Global Definitions
 ****************************************************************************/

/*****************************************************************************
 * Structure Definitions
 ****************************************************************************/

/*****************************************************************************
 * DS Template Type Definitions
 ****************************************************************************/
 /*****************************************
 * TYPEDEF for Data Structure
 *    Template Name: Read XML Data & Copy into table                            
 *    Template ID:   D5601XML
 *    Generated:     Wed Nov 10 14:50:14 2010
 *
 * DO NOT EDIT THE FOLLOWING TYPEDEF
 *    To make modifications, use the OneWorld Data Structure
 *    Tool to Generate a revised version, and paste from
 *    the clipboard.
 *
 **************************************/

#ifndef DATASTRUCTURE_D5601XML
#define DATASTRUCTURE_D5601XML

typedef struct tagDSD5601XML
{
  JCHAR             szXMLPath[251];                    
  JCHAR             szErrorMessage[101];                
  JCHAR             szFlatFilePath[256];                
} DSD5601XML, *LPDSD5601XML;

#define IDERRszXMLPath_1                          1L
#define IDERRszErrorMessage_3                     3L
#define IDERRszFlatFilePath_4                     4L

#endif

/*****************************************************************************
 * Source Preprocessor Definitions
 ****************************************************************************/
#if defined (JDEBFRTN)
#undef JDEBFRTN
#endif

#if defined (WIN32)
#if defined (WIN32)
#define JDEBFRTN(r) __declspec(dllexport) r
#else
#define JDEBFRTN(r) __declspec(dllimport) r
#endif
#else
#define JDEBFRTN(r) r
#endif

/*****************************************************************************
 * Business Function Prototypes
 ****************************************************************************/
 JDEBFRTN (ID) JDEBFWINAPI CopyXMLDataToTable               (LPBHVRCOM lpBhvrCom, LPVOID lpVoid, LPDSD5601XML lpDS);


/*****************************************************************************
 * Internal Function Prototypes
 ****************************************************************************/

#endif    /* __B5601XML_H */

#include <jde.h>

#define B5601XML_c


/*****************************************************************************
 *    Source File:  B5601XML
 *
 *    Description:  Read XML Data & Copy into Flat File
 *
 *        History:
 *          Date        Programmer  Email
 *          ----------  ----------  -------------------------------------------
 *   Author 11/10/2015  Lokesh Reddy        - Created  
 *
 * Copyright (c) J.D. Edwards World Source Company, 1996
 *
 * This unpublished material is proprietary to J.D. Edwards World Source Company.
 * All rights reserved.  The methods and techniques described herein are
 * considered trade secrets and/or confidential.  Reproduction or
 * distribution, in whole or in part, is forbidden except by express
 * written permission of J.D. Edwards World Source Company.
 ****************************************************************************/
/**************************************************************************
 * Notes:
 *
 **************************************************************************/

#include <B5601XML.h>


/**************************************************************************
 *  Business Function:  CopyXMLDataToTable
 *
 *        Description:  Copy XML Data into Flat File
 *
 *         Parameters:
 *           LPBHVRCOM           lpBhvrCom    Business Function Communications
 *           LPVOID              lpVoid       Void Parameter - DO NOT USE!
 *           LPDSD560            lpDS         Parameter Data Structure Pointer  
 *
 *************************************************************************/

JDEBFRTN (ID) JDEBFWINAPI CopyXMLDataToTable (LPBHVRCOM lpBhvrCom, LPVOID lpVoid, LPDSD5601XML lpDS)

{
#ifdef SOA_ENABLED
   /************************************************************************
    *  Variable declarations
    ************************************************************************/
XRCS_hParser hParser = NULL;
XRCS_Status XRCSStatus = XRCS_SUCCESS;
XRCS_hDocument hDoc = NULL;
XRCS_hElement hRootElm = NULL;
XRCS_hElement *hElm = NULL;
unsigned int nElmCount = 0;
XRCS_hElement *hChldElm = NULL;
unsigned int nChldElmCount = 0;
JCHAR *elmName = NULL;
JCHAR *chldElmName = NULL;
JCHAR *chldElmValue = NULL;
unsigned int i = 0;
unsigned int j = 0;
FILE *fpFlat;
/************************************************************************
    * Declare structures
    ************************************************************************/

   /************************************************************************
    * Declare pointers
    ************************************************************************/

   /************************************************************************
    * Check for NULL pointers
    ************************************************************************/
   if ((lpBhvrCom == (LPBHVRCOM) NULL) ||
       (lpVoid    == (LPVOID)    NULL) ||
       (lpDS      == (LPDSD5601XML) NULL))
   {
     jdeErrorSet (lpBhvrCom, lpVoid, (ID) 0, _J("4363"), (LPVOID) NULL);
     return ER_ERROR;
   }

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

   /************************************************************************
    * Main Processing
    ************************************************************************/
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J(" "));

/*Changed file opening mode to binary. Previously it was "a+t"*/
if( (fpFlat = jdeFopen(lpDS->szFlatFilePath, _J("wb") )) == NULL )
{
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("Flat File open failed"));
return ER_ERROR;
}

/* Initialize Xerces Engine */
XRCSStatus = XRCS_initEngine();
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_initEngine failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_initEngine failed"));
jdeFclose(fpFlat);
return ER_ERROR;
}

/* Get the Xerces Parser */
XRCSStatus = XRCS_getParser(&hParser);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_getParser failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_getParser failed"));
XRCS_terminateEngine();
jdeFclose(fpFlat);
return ER_ERROR;
}

/* Parse the XML String */
XRCSStatus = XRCS_parseXMLFile(hParser,lpDS->szXMLPath, &hDoc);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_parseXMLFile failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_parseXMLFile failed"));
XRCS_freeParser(hParser);
XRCS_terminateEngine();
jdeFclose(fpFlat);
return ER_ERROR;
}

/* Get Root Element */
XRCSStatus = XRCS_getDocumentElement(hDoc,&hRootElm);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_getDocumentElement failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_getDocumentElement failed"));
XRCS_freeParser(hParser);
XRCS_freeDocument(hDoc);
XRCS_terminateEngine();
jdeFclose(fpFlat);
return ER_ERROR;
}

/* Get Elements in XML Document*/
XRCSStatus = XRCS_getElementChildren(hRootElm,&hElm,&nElmCount);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_getElementChildren failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_getElementChildren failed"));
XRCS_freeParser(hParser);
XRCS_freeDocument(hDoc);
XRCS_freeElement(hRootElm);
XRCS_terminateEngine();
jdeFclose(fpFlat);
return ER_ERROR;
}

for(i=0;i<nElmCount;i++)
{

XRCSStatus = XRCS_getElementName(hElm[i],&elmName);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_getElementName failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_getElementName failed"));
XRCS_freeParser(hParser);
XRCS_freeDocument(hDoc);
XRCS_freeElementArray(hElm,nElmCount);
XRCS_freeElement(hRootElm);
XRCS_terminateEngine();
jdeFclose(fpFlat);
return ER_ERROR;
}

XRCSStatus = XRCS_getElementChildren(hElm[i],&hChldElm,&nChldElmCount);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_getElementChildren failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_getElementChildren failed"));
XRCS_freeParser(hParser);
XRCS_freeDocument(hDoc);
XRCS_freeElement(hRootElm);
XRCS_freeElementArray(hElm,nElmCount);
XRCS_terminateEngine();
jdeFclose(fpFlat);
return ER_ERROR;
}

for(j=0;j<nChldElmCount;j++)
{
XRCSStatus = XRCS_getElementName(hChldElm[j],&chldElmName);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_getElementName failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_getElementName failed"));
XRCS_freeParser(hParser);
XRCS_freeDocument(hDoc);
XRCS_freeElementArray(hElm,nElmCount);
XRCS_freeElementArray(hChldElm,nChldElmCount);
XRCS_freeElement(hRootElm);
XRCS_freeElement(elmName);
XRCS_terminateEngine();
jdeFclose(fpFlat);
return ER_ERROR;
}

XRCSStatus = XRCS_getElementText(hChldElm[j],&chldElmValue);
if(XRCSStatus != XRCS_SUCCESS)
{
jdeVWriteLogEntry(_J("B5601XML"), __FILE__, __LINE__, 0, _J("XRCS_getElementText failed"));
jdeStrcpy((JCHAR *)lpDS->szErrorMessage,(const JCHAR *)_J("XRCS_getElementText failed"));
XRCS_freeParser(hParser);
XRCS_freeDocument(hDoc);
XRCS_freeElementArray(hElm,nElmCount);
XRCS_freeElementArray(hChldElm,nChldElmCount);
XRCS_freeElement(hRootElm);
XRCS_terminateEngine();
XRCS_freeString(elmName);
XRCS_freeString(chldElmName);
jdeFclose(fpFlat);
return ER_ERROR;
}
if ( (JCHAR*)NULL != chldElmValue)
{
jdeFputsConvert(lpBhvrCom, chldElmValue, fpFlat);

if((j+1)<nChldElmCount){
jdeFputsConvertCRLF(lpBhvrCom, _J(","), fpFlat);
}
XRCS_freeString(chldElmName);
XRCS_freeString(chldElmValue);
chldElmName = (JCHAR*)NULL;
chldElmValue = (JCHAR*)NULL;
}
}
jdeFputsConvertCRLF(lpBhvrCom, _J("\r\n"), fpFlat);


} /*end of for loop*/

   /************************************************************************
    * Function Clean Up
    ************************************************************************/
XRCS_freeParser(hParser);
XRCS_freeDocument(hDoc);
XRCS_freeElementArray(hElm,nElmCount);
XRCS_freeElementArray(hChldElm,nChldElmCount);
XRCS_freeElement(hRootElm);
jdeFclose(fpFlat);
XRCS_terminateEngine();
 
#else
jdeTraceSz(NULL, _J("B5601XML - CopyXMLDataToTable called but system does not have SOA_ENABLED defined."));
#endif
   return (ER_SUCCESS);
}

/* Internal function comment block */
/**************************************************************************
 *   Function:  Ixxxxxxx_a   // Replace "xxxxxxx" with source file number
 *                           // and "a" with the function name
 *      Notes:
 *
 *    Returns:
 *
 * Parameters:
 **************************************************************************/



4 comments:

  1. perfect work !! could you send the xml file? thank you !!

    ReplyDelete
  2. kqlandia, work good? i need read a xml file

    ReplyDelete
  3. I tried this code, but a blank flat file is getting created. Did it work for anyone?

    ReplyDelete
  4. need to declare the library #include

    ReplyDelete