XML Read Problems
Posted: Tue Apr 26, 2016 1:19 am
Hello Everyone,
I am having inconsistent problems reading a XML file via Harbour. I wanted to get your assistance reading XML node. I have attached my source, your feedback is much appreciated.
#include "FiveWin.ch"
static atzones := { ;
{ "ET", -5, -4 }, ; // Eastern
{ "CT", -6, -5 }, ; // Central
{ "MT", -7, -6 }, ; // Mountain
{ "PT", -8, -7 }, ; // Pacific
{ "AT", -9, -8 }, ; // Alaska
{ "HT", -10, -10 }, ; // Hawaii
{ "AR", -7, -7 }, ; // Arizona
{ "SGT", 8, 8 }, ; // Singapore
{ "HKT", 8, 8 } } // Hongkong
static aDsTable := { ;
{ 2015, {^ 2015/03/08 02:00:00 }, {^ 2015/11/01 02:00:00 } }, ;
{ 2016, {^ 2016/03/13 02:00:00 }, {^ 2016/11/06 02:00:00 } }, ;
{ 2017, {^ 2017/03/12 02:00:00 }, {^ 2017/11/05 02:00:00 } }, ;
{ 2018, {^ 2018/03/11 02:00:00 }, {^ 2018/11/04 02:00:00 } }, ;
{ 2019, {^ 2019/03/10 02:00:00 }, {^ 2019/11/03 02:00:00 } } }
REQUEST DBFCDX
REQUEST DBFFPT
//-----------------------------------------------------------------------------
function Main( cQuery )
local hFile , ;
oXmlDoc , ;
oXmlIter , ;
oTagActual , ;
nItems := 0 , ;
cTagName := "" , ;
cTagData := "" , ;
cMtsn := "" , ;
cAssetid := "" , ;
cMsgtype := "" , ;
cEventsource := "" , ;
cExtpwr := "" , ;
cSerialtype := "" , ;
cSeralid := "" , ;
cSerialname := "" , ;
cSerialdata := "" , ;
cTrid := "" , ;
cLatitude := "" , ;
cLongitude := "" , ;
cBattery := "" , ;
cTimeSave := "" , ;
cTime := "" , ;
dDate := CTOD( " / / " ) , ;
cQuality := "" , ;
cGeoname := "" , ;
cState := "" , ;
cCountry := "" , ;
cDistance := "" , ;
oSql , ;
oSql2 , ;
cSqlIns , ;
cSqlUpdate , ;
dStartDate := CTOD("03/14/2016") , ;
cLocation1 := "" , ;
cLocation2 := "" , ;
lAtdepot := .F. , ;
cCurrHub := '' , ;
cDepotname := '' , ;
nCtnrRec := 0 , ;
lPass := .F.
IF PCOUNT()<1
cQuery := ""
ENDIF
// Global Vars...
PUBLIC Ptitle := "GPS" , ;
Pbuild := "20160320-2158" , ;
cEol := CHR(13) + CHR(10) , ;
cSource := "c:\xml\inbox\" , ;
cHistory := "c:\xml\inbox\history\" , ;
xPROVIDER := "SQLOLEDB" , ;
xSOURCE := "9.9.9.9" , ;
xDATABASE := "DB-NAME" , ;
xUSERID := "USERID" , ;
xPASSWORD := "PASSWORD" , ;
cOperations := "<operations@cdmsoft.com>" , ;
cPath := 'c:\data\' , ;
lAds := .F. , ;
cRevised := "March 20, 2016 - 1745"
RddSetDefault( 'DBFCDX' )
// XML Query...
IF EMPTY( cQuery )
IF ! _XmlQuery()
QUIT
ENDIF
ENDIF
// Open...
_db( cPath, 'edi315' )
_db( cPath, 'depot' )
_db( cPath, 'ctnr' )
_db( cPath, 'ctnrtrx' )
_db( cPath, 'perdiem' )
_db( cPath, 'user' )
aXmlFile := Directory( cSource + "*.XML" )
IF LEN(aXmlFile)>0
cXmlFile := aXmlFile[1,1]
hFile := FOpen( cSource + cXmlFile )
oXmlDoc := TXmlDocument():New( hFile )
oXmlIter := TXmlIterator():New( oXmlDoc:oRoot )
WHILE .T.
oTagActual = oXmlIter:Next()
If oTagActual != nil
//MsgInfo( oTagActual:cName, oTagActual:cData )
// Init...
cTagName := ""
cTagData := ""
IF (oTagActual:cName != nil)
cTagName := oTagActual:cName
ENDIF
IF (oTagActual:cData != nil)
cTagData := oTagActual:cData
ENDIF
IF UPPER(cTagName)=="MTSN"
cMtsn := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="ASSETID"
cAssetid := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="MESSAGETYPE"
cMsgtype := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="EVENTSOURCE"
cEventsource := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="EXTPWR"
cExtpwr := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALTYPE"
cSerialtype := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALID"
cSerialid := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALNAME"
cSerialname := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALDATA"
cSerialdata := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="TRID"
cTrid := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="LATITUDE"
cLatitude := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="LONGITUDE"
cLongitude := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="BATTERY"
cBattery := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="TIME"
cTime := _SqlClean( UPPER( cTagData ) )
cTimeSave := cTime
dDate := CTOD( SUBSTR( cTime, 6, 5 ) + "/" + LEFT(cTime, 4 ) )
cTime := SUBSTR( cTime, 12 )
ENDIF
IF UPPER(cTagName)=="QUALITY"
cQuality := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="GEONAME"
cGeoname := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="STATE"
cState := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="COUNTRY"
cCountry := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="DISTANCE"
cDistance := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="GLS"
IF (! EMPTY( cAssetid )) .AND. (dDate>=dStartdate) .AND. (! LEFT(cAssetid,4)=="TESU")
// Init...
lAtDepot := .F.
cCurrHub := ''
cDepotname := ''
nCtnrRec := 0
cReqno := ''
cTimezone := ''
IF (nCtnrRec>0)
SELECT ctnrtrx
GO nCtnrRec
IF (ALLTRIM(cAssetid)==ALLTRIM(ctnrtrx->ctnrno)) .AND. (EMPTY(ctnrtrx->termdate)) .AND. (! EMPTY(ctnrtrx->resvdate)) .AND. (!EMPTY(ctnrtrx->ingate)) .AND. (!EMPTY(ctnrtrx->notify)) .AND. (!EMPTY(ctnrtrx->outgate))
DO WHILE (.T.)
IF ctnrtrx->( dbrlock() )
ctnrtrx->( dbrunlock() )
EXIT
ENDIF
ENDDO
ENDIF
SELECT edi315
APPEND BLANK
edi315->isa := 'HEREGPS'
edi315->ctnr := ALLTRIM( cAssetid )
edi315->reqno := ALLTRIM( cReqno )
edi315->b4 := IF(lAtDepot,'T','X')
edi315->b4d := dDate
edi315->b4t := ALLTRIM( cTime )
edi315->r4e := ALLTRIM( cGeoname )
edi315->r4d := dDate
edi315->r4t := ALLTRIM( cTime )
edi315->mstn := ALLTRIM( cMtsn )
edi315->msgtype := ALLTRIM( cMsgtype )
edi315->eventsrc := ALLTRIM( cEventsource )
edi315->extpwr := ALLTRIM( cExtpwr )
edi315->serialtype := ALLTRIM( cSerialtype )
edi315->serialid := ALLTRIM( cSerialid )
edi315->serialname := ALLTRIM( cSerialname )
edi315->serialdata := ALLTRIM( cSerialdata )
edi315->trid := ALLTRIM( cTrid )
edi315->latitude := ALLTRIM( cLatitude )
edi315->longitude := ALLTRIM( cLongitude )
edi315->battery := ALLTRIM( cBattery )
edi315->quality := ALLTRIM( cQuality )
edi315->landmark := ALLTRIM( cGeoname )
edi315->landmarkst := ALLTRIM( cState )
edi315->landmarkis := ALLTRIM( cCountry )
edi315->landmarkdi := ALLTRIM( cDistance )
edi315->txdate := DATE()
edi315->txtime := TIME()
edi315->timesave := cTimeSave
nItems++
// Init...
cMtsn := ""
cAssetid := ""
cMsgtype := ""
cEventsource := ""
cExtpwr := ""
cSerialtype := ""
cSeralid := ""
cSerialname := ""
cSerialdata := ""
cTrid := ""
cLatitude := ""
cLongitude := ""
cBattery := ""
cTime := ""
cTimeSave := ""
dDate := CTOD( " / / " )
cQuality := ""
cGeoname := ""
cState := ""
cCountry := ""
cDistance := ""
ENDIF
ENDIF
HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue ) } )
Else
Exit
Endif
End
FClose( hFile )
// Copy to History...
COPY FILE (cSource + cXmlFile) TO (cHistory + cXmlFile)
IF FILE( cHistory + cXmlFile )
FERASE( cSource + cXmlFile )
ENDIF
ENDIF
DbCloseAll()
MsgWait( 'Records Added = ' + LTRIM(STR(nItems,9)), Ptitle, 5 )
return nil
I am having inconsistent problems reading a XML file via Harbour. I wanted to get your assistance reading XML node. I have attached my source, your feedback is much appreciated.
#include "FiveWin.ch"
static atzones := { ;
{ "ET", -5, -4 }, ; // Eastern
{ "CT", -6, -5 }, ; // Central
{ "MT", -7, -6 }, ; // Mountain
{ "PT", -8, -7 }, ; // Pacific
{ "AT", -9, -8 }, ; // Alaska
{ "HT", -10, -10 }, ; // Hawaii
{ "AR", -7, -7 }, ; // Arizona
{ "SGT", 8, 8 }, ; // Singapore
{ "HKT", 8, 8 } } // Hongkong
static aDsTable := { ;
{ 2015, {^ 2015/03/08 02:00:00 }, {^ 2015/11/01 02:00:00 } }, ;
{ 2016, {^ 2016/03/13 02:00:00 }, {^ 2016/11/06 02:00:00 } }, ;
{ 2017, {^ 2017/03/12 02:00:00 }, {^ 2017/11/05 02:00:00 } }, ;
{ 2018, {^ 2018/03/11 02:00:00 }, {^ 2018/11/04 02:00:00 } }, ;
{ 2019, {^ 2019/03/10 02:00:00 }, {^ 2019/11/03 02:00:00 } } }
REQUEST DBFCDX
REQUEST DBFFPT
//-----------------------------------------------------------------------------
function Main( cQuery )
local hFile , ;
oXmlDoc , ;
oXmlIter , ;
oTagActual , ;
nItems := 0 , ;
cTagName := "" , ;
cTagData := "" , ;
cMtsn := "" , ;
cAssetid := "" , ;
cMsgtype := "" , ;
cEventsource := "" , ;
cExtpwr := "" , ;
cSerialtype := "" , ;
cSeralid := "" , ;
cSerialname := "" , ;
cSerialdata := "" , ;
cTrid := "" , ;
cLatitude := "" , ;
cLongitude := "" , ;
cBattery := "" , ;
cTimeSave := "" , ;
cTime := "" , ;
dDate := CTOD( " / / " ) , ;
cQuality := "" , ;
cGeoname := "" , ;
cState := "" , ;
cCountry := "" , ;
cDistance := "" , ;
oSql , ;
oSql2 , ;
cSqlIns , ;
cSqlUpdate , ;
dStartDate := CTOD("03/14/2016") , ;
cLocation1 := "" , ;
cLocation2 := "" , ;
lAtdepot := .F. , ;
cCurrHub := '' , ;
cDepotname := '' , ;
nCtnrRec := 0 , ;
lPass := .F.
IF PCOUNT()<1
cQuery := ""
ENDIF
// Global Vars...
PUBLIC Ptitle := "GPS" , ;
Pbuild := "20160320-2158" , ;
cEol := CHR(13) + CHR(10) , ;
cSource := "c:\xml\inbox\" , ;
cHistory := "c:\xml\inbox\history\" , ;
xPROVIDER := "SQLOLEDB" , ;
xSOURCE := "9.9.9.9" , ;
xDATABASE := "DB-NAME" , ;
xUSERID := "USERID" , ;
xPASSWORD := "PASSWORD" , ;
cOperations := "<operations@cdmsoft.com>" , ;
cPath := 'c:\data\' , ;
lAds := .F. , ;
cRevised := "March 20, 2016 - 1745"
RddSetDefault( 'DBFCDX' )
// XML Query...
IF EMPTY( cQuery )
IF ! _XmlQuery()
QUIT
ENDIF
ENDIF
// Open...
_db( cPath, 'edi315' )
_db( cPath, 'depot' )
_db( cPath, 'ctnr' )
_db( cPath, 'ctnrtrx' )
_db( cPath, 'perdiem' )
_db( cPath, 'user' )
aXmlFile := Directory( cSource + "*.XML" )
IF LEN(aXmlFile)>0
cXmlFile := aXmlFile[1,1]
hFile := FOpen( cSource + cXmlFile )
oXmlDoc := TXmlDocument():New( hFile )
oXmlIter := TXmlIterator():New( oXmlDoc:oRoot )
WHILE .T.
oTagActual = oXmlIter:Next()
If oTagActual != nil
//MsgInfo( oTagActual:cName, oTagActual:cData )
// Init...
cTagName := ""
cTagData := ""
IF (oTagActual:cName != nil)
cTagName := oTagActual:cName
ENDIF
IF (oTagActual:cData != nil)
cTagData := oTagActual:cData
ENDIF
IF UPPER(cTagName)=="MTSN"
cMtsn := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="ASSETID"
cAssetid := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="MESSAGETYPE"
cMsgtype := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="EVENTSOURCE"
cEventsource := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="EXTPWR"
cExtpwr := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALTYPE"
cSerialtype := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALID"
cSerialid := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALNAME"
cSerialname := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="SERIALDATA"
cSerialdata := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="TRID"
cTrid := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="LATITUDE"
cLatitude := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="LONGITUDE"
cLongitude := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="BATTERY"
cBattery := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="TIME"
cTime := _SqlClean( UPPER( cTagData ) )
cTimeSave := cTime
dDate := CTOD( SUBSTR( cTime, 6, 5 ) + "/" + LEFT(cTime, 4 ) )
cTime := SUBSTR( cTime, 12 )
ENDIF
IF UPPER(cTagName)=="QUALITY"
cQuality := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="GEONAME"
cGeoname := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="STATE"
cState := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="COUNTRY"
cCountry := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="DISTANCE"
cDistance := _SqlClean( UPPER( cTagData ) )
ENDIF
IF UPPER(cTagName)=="GLS"
IF (! EMPTY( cAssetid )) .AND. (dDate>=dStartdate) .AND. (! LEFT(cAssetid,4)=="TESU")
// Init...
lAtDepot := .F.
cCurrHub := ''
cDepotname := ''
nCtnrRec := 0
cReqno := ''
cTimezone := ''
IF (nCtnrRec>0)
SELECT ctnrtrx
GO nCtnrRec
IF (ALLTRIM(cAssetid)==ALLTRIM(ctnrtrx->ctnrno)) .AND. (EMPTY(ctnrtrx->termdate)) .AND. (! EMPTY(ctnrtrx->resvdate)) .AND. (!EMPTY(ctnrtrx->ingate)) .AND. (!EMPTY(ctnrtrx->notify)) .AND. (!EMPTY(ctnrtrx->outgate))
DO WHILE (.T.)
IF ctnrtrx->( dbrlock() )
ctnrtrx->( dbrunlock() )
EXIT
ENDIF
ENDDO
ENDIF
SELECT edi315
APPEND BLANK
edi315->isa := 'HEREGPS'
edi315->ctnr := ALLTRIM( cAssetid )
edi315->reqno := ALLTRIM( cReqno )
edi315->b4 := IF(lAtDepot,'T','X')
edi315->b4d := dDate
edi315->b4t := ALLTRIM( cTime )
edi315->r4e := ALLTRIM( cGeoname )
edi315->r4d := dDate
edi315->r4t := ALLTRIM( cTime )
edi315->mstn := ALLTRIM( cMtsn )
edi315->msgtype := ALLTRIM( cMsgtype )
edi315->eventsrc := ALLTRIM( cEventsource )
edi315->extpwr := ALLTRIM( cExtpwr )
edi315->serialtype := ALLTRIM( cSerialtype )
edi315->serialid := ALLTRIM( cSerialid )
edi315->serialname := ALLTRIM( cSerialname )
edi315->serialdata := ALLTRIM( cSerialdata )
edi315->trid := ALLTRIM( cTrid )
edi315->latitude := ALLTRIM( cLatitude )
edi315->longitude := ALLTRIM( cLongitude )
edi315->battery := ALLTRIM( cBattery )
edi315->quality := ALLTRIM( cQuality )
edi315->landmark := ALLTRIM( cGeoname )
edi315->landmarkst := ALLTRIM( cState )
edi315->landmarkis := ALLTRIM( cCountry )
edi315->landmarkdi := ALLTRIM( cDistance )
edi315->txdate := DATE()
edi315->txtime := TIME()
edi315->timesave := cTimeSave
nItems++
// Init...
cMtsn := ""
cAssetid := ""
cMsgtype := ""
cEventsource := ""
cExtpwr := ""
cSerialtype := ""
cSeralid := ""
cSerialname := ""
cSerialdata := ""
cTrid := ""
cLatitude := ""
cLongitude := ""
cBattery := ""
cTime := ""
cTimeSave := ""
dDate := CTOD( " / / " )
cQuality := ""
cGeoname := ""
cState := ""
cCountry := ""
cDistance := ""
ENDIF
ENDIF
HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue ) } )
Else
Exit
Endif
End
FClose( hFile )
// Copy to History...
COPY FILE (cSource + cXmlFile) TO (cHistory + cXmlFile)
IF FILE( cHistory + cXmlFile )
FERASE( cSource + cXmlFile )
ENDIF
ENDIF
DbCloseAll()
MsgWait( 'Records Added = ' + LTRIM(STR(nItems,9)), Ptitle, 5 )
return nil