Processing XML Data

Processing XML Data

Postby cdmmaui » Wed Mar 13, 2013 11:32 am

Hello,

Using your example below, how would I determine when all XML data for node is complete so I can perform the validation checks on XML data assigned to variables and either create a new record or update an existing record?

If possible, could you give a small sample?

Thank you,

//----------------------------------------------------------------
// Open...
hFile := FOPEN( cSource+cFileName )
// Init...
oXmlDoc := TXmlDocument():New( hFile )
oXmlIter := TXmlIterator():New( oXmlDoc:oRoot )
WHILE .T.
oTagActual = oXmlIter:Next()
If oTagActual != nil
_XmlParse( oTagActual:cName, oTagActual:cData, "", @lPass, cSource+cFileName, @cEdiLog )
HEval( oTagActual:aAttributes, { | cKey, cValue | _XmlParse( cKey, cValue, oTagActual:cName, @lPass, cSOurce+cFileName, @cEdiLog ) } )
ELSE
EXIT
ENDIF
END
FClose( hFile )
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: Processing XML Data

Postby Antonio Linares » Wed Mar 13, 2013 12:18 pm

Darrell,

This is the structure of the XML that you emailed me (inspected with FWH\samples\xmltree.prg):

Image

I only see one <detail> record into it. Could it have more than one ?

Based on this image, what record fields are planning to use ? Which ones are you already using ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Processing XML Data

Postby Antonio Linares » Wed Mar 13, 2013 12:23 pm

Darrell,

Each node (in the code we call them "tag") has a Depth(), so once you find "Detail", the next will have a Depth() + 1, so you iterate through them until the Depth() becomes the same as the one that "Detail" has :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Processing XML Data

Postby cdmmaui » Wed Mar 13, 2013 12:27 pm

Hi Antonio,

I will need to process the following data for each DETAIL (including attributes)

1. Identifiers
2. Military
3. ShipmentStatus
4. ShipperDetails
5. Origin (TransitXMLKey, Identifiers, Physical, Facts, ContactDetails (3) )
6. Destination
7. Physical
8. Dates
9. ContactDetails
10. TransportItem
11. Transit (Identifiers, Dates, ShippingDetails)

I am sending you XML with multiple DETAIL nodes.
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: Processing XML Data

Postby cdmmaui » Wed Mar 13, 2013 12:33 pm

Hi Antonio,

I how would I change the code below to handle this?

// Open...
hFile := FOPEN( cSource+cFileName )
// Init...
oXmlDoc := TXmlDocument():New( hFile )
oXmlIter := TXmlIterator():New( oXmlDoc:oRoot )
WHILE .T.
oTagActual = oXmlIter:Next()
If oTagActual != nil
_XmlParse( oTagActual:cName, oTagActual:cData, "", @lPass, cSource+cFileName, @cEdiLog )
HEval( oTagActual:aAttributes, { | cKey, cValue | _XmlParse( cKey, cValue, oTagActual:cName, @lPass, cSOurce+cFileName, @cEdiLog ) } )
ELSE
EXIT
ENDIF
END
FClose( hFile )
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: Processing XML Data

Postby Antonio Linares » Wed Mar 13, 2013 12:35 pm

Darrell,

This example shows how to iterate those nodes: (I have renamed your XML to test.xml)

darrell.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()
   
   local oXmlDoc  := TXmlDocument():New( "test.xml" )
   local oXmlIter := TXmlIterator():New( oXmlDoc:oRoot ), oTagActual
   local nDepth

   while .T.
      oTagActual = oXmlIter:Next()
      if oTagActual != nil
         if oTagActual:cName == "Detail"
            nDepth = oTagActual:Depth()
            while ( oTagActual := oXmlIter:Next() ) != nil .and. oTagActual:Depth() > nDepth
               if oTagActual:Depth() == nDepth + 1
                  MsgInfo( oTagActual:cName )
               endif  
            end
         endif    
      else
         exit
      endif
   end

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Processing XML Data

Postby Antonio Linares » Wed Mar 13, 2013 12:44 pm

This is the idea that I commented to you to automatically fill the DBF. Incredibly simpler than your code ;-)

This is the key:
FieldPut( FieldPos( oTagActual:cName ), oTagActual:cData )

darrell2.prg
Code: Select all  Expand view
#include "FiveWin.ch"

function Main()
   
   local oXmlDoc  := TXmlDocument():New( "test.xml" )
   local oXmlIter := TXmlIterator():New( oXmlDoc:oRoot ), oTagActual
   local nDepth

   while .T.
      oTagActual = oXmlIter:Next()
      if oTagActual != nil
         if oTagActual:cName == "Detail"
            nDepth = oTagActual:Depth()
            while ( oTagActual := oXmlIter:Next() ) != nil .and. oTagActual:Depth() > nDepth
               FieldPut( FieldPos( oTagActual:cName ), oTagActual:cData )
            end
         endif    
      else
         exit
      endif
   end

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Processing XML Data

Postby cdmmaui » Wed Mar 13, 2013 12:56 pm

Hi Antonio,

I get the following error when trying to process XML with a single DETAIL

Application
===========
Path and name: C:\Winapps\vessel\testxml.exe (32 bits)
Size: 1,981,440 bytes
Compiler version: xHarbour build 1.2.1 Intl. (SimpLex) (Rev. 9656)
FiveWin Version: FWHX 12.08
Windows version: 6.1, Build 7601 Service Pack 1

Time from start: 0 hours 0 mins 4 secs
Error occurred at: 03/13/13, 07:55:34
Error description: Error BASE/1004 Class: 'NIL' has no exported method: NEXTINTREE
Args:
[ 1] = U

Stack Calls
===========
Called from: => NEXTINTREE( 0 )
Called from: source\rtl\txml.prg => TXMLITERATOR:NEXT( 0 )
Called from: testxml.prg => MAIN( 14 )

System
======
CPU type: Intel(R) Core(TM) i7-2960XM CPU @ 2.70GHz 2700 Mhz
Hardware memory: 3318 megs

Free System resources: 90 %
GDI resources: 90 %
User resources: 90 %

Windows total applications running: 41
1 , C:\Program Files\ATI Technologies\ATI.ACE\Core-Static\CCC.exe
2 , C:\Program Files\Citrix\GoToMeeting\1133\g2mlauncher.exe
3 , C:\Program Files\Citrix\GoToMeeting\1133\g2mstart.exe
4 , C:\Program Files\Common Files\Intel\WirelessCommon\iFrmewrk.exe
5 , C:\Program Files\DellTPad\Apoint.exe
6 , C:\Program Files\EMS\SQL Studio for MySQL\Studio\myagent.exe
7 , C:\Program Files\EMS\SQL Studio for SQL Server\Studio\msagent.exe
8 , C:\Program Files\HP\Digital Imaging\bin\hpqtra08.exe
9 , C:\Program Files\Intel\Intel(R) Management Engine Components\IMSS\PrivacyIconClient.exe
10 , C:\Program Files\Intel\Intel(R) Rapid Storage Technology\IAStorIcon.exe
11 , C:\Program Files\Intel\WiMAX\Bin\WiMAXCU.exe
12 , C:\Program Files\Internet Explorer\iexplore.exe
13 , C:\Program Files\Microsoft Device Center\ipoint.exe
14 , C:\Program Files\Microsoft Device Center\itype.exe
15 , C:\Program Files\Microsoft Office\Office14\OUTLOOK.EXE
16 , C:\Program Files\NovaStor\NovaStor NovaBACKUP\nsCtrl.exe
17 , C:\Windows\Explorer.EXE
18 , C:\Windows\System32\mobsync.exe
19 , C:\Windows\WindowsMobile\wmdc.exe
20 .NET-BroadcastEventWindow.2.0.0.0.5c39d4.0, c:\Program Files\ATI Technologies\ATI.ACE\Core-Static\MOM.exe
21 ApUsbPnP, C:\Program Files\DellTPad\HidFind.exe
22 C:\Program Files\DellTPad\Apntex.exe, C:\Program Files\DellTPad\Apntex.exe
23 CL RC Engine3 Dummy Winidow, C:\Program Files\CyberLink\PowerDVD9\PDVD9Serv.exe
24 CTrayNotifyIcon Helper Window, C:\Program Files\Dell\Feature Enhancement Pack\DFEPApplication.exe
25 CiceroUIWndFrame, C:\Windows\system32\taskhost.exe
26 DWM Notification Window, C:\Windows\system32\Dwm.exe
27 Dell Display Manager, C:\Program Files\Dell\Dell Display Manager\ddm.exe
28 Dell Smart Settings, C:\Program Files\Dell\Feature Enhancement Pack\SmartSettings.exe
29 EBBF641A-115F-411B-AF4C-08235A087962, C:\Program Files\EMS\SQL Studio for MySQL\Studio\MyService.exe
30 FFB7516C-BD23-4D55-9F4F-F743B20F444E, C:\Program Files\EMS\SQL Studio for SQL Server\Studio\MsService.exe
31 Free Fall Data Protection, C:\Program Files\STMicroelectronics\AccelerometerP11\FF_Protection.exe
32 GDI+ Window, C:\Program Files\Ipswitch\WS_FTP 12\WsftpCOMHelper.exe
33 HPWU, C:\Program Files\HP\HP Software Update\hpwuschd2.exe
34 MSCTFIME UI, C:\Windows\system32\conhost.exe
35 Netsession Hidden Window, C:\Users\cdm\AppData\Local\Akamai\netsession_win.exe
36 Sigmatel-IDT-SysTray, C:\Program Files\IDT\WDM\sttray.exe
37 TdmNotify, C:\Program Files\Dell\Dell Data Protection\Access\Advanced\Wave\Trusted Drive Manager\TdmNotify.exe
38 WinAMRestoreWnd, C:\Program Files\iTunes\iTunesHelper.exe
39 cmd, C:\Windows\System32\cmd.exe
40 zvprtsrv, C:\Program Files\HP\HP Color LaserJet CM2320 MFP Series\hppfaxprintersrv.exe
41 {2E534F4D-897B-467E-92EF-604ABD3D1C5D},

Variables in use
================
Procedure Type Value
==========================
NEXTINTREE
Local 1: A Len: 0
Local 2: U
Local 3: U
TXMLITERATOR:NEXT
Local 1: O Class: TXMLITERATOR
Local 2: U
MAIN
Local 1: O Class: TXMLDOCUMENT
Local 2: O Class: TXMLITERATOR
Local 3: U
Local 4: N 2

Linked RDDs
===========
DBF
DBFFPT
DBFBLOB
DBFNTX

DataBases in use
================

Classes in use:
===============
1 ERROR
2 HASHENTRY
3 HBCLASS
4 HBOBJECT
5 TXMLDOCUMENT
6 TXMLNODE
7 TXMLITERATOR
8 TREG32

Memory Analysis
===============
166 Static variables

Dynamic memory consume:
Actual Value: 0 bytes
Highest Value: 0 bytes

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: Processing XML Data

Postby Antonio Linares » Wed Mar 13, 2013 1:01 pm

Darrell,

Its a xharbour bug.

Please build it using Harbour doing FWH\samples\buildh.bat darrell
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Processing XML Data

Postby cdmmaui » Wed Mar 13, 2013 1:36 pm

Hi Antonio,

Thank you that worked. I now need pull the attribute values within detail, I tried the following two options with no luck. Can you tell what I am doing wrong?

// Init...
oXmlDoc := TXmlDocument():New( cSource+cFileName )
oXmlIter := TXmlIterator():New( oXmlDoc:oRoot )

WHILE .T.
oTagActual = oXmlIter:Next()
IF oTagActual != nil
if oTagActual:cName == "Detail"
nDepth = oTagActual:Depth()
while ( oTagActual := oXmlIter:Next() ) != nil .and. oTagActual:Depth() > nDepth
if oTagActual:Depth() == nDepth + 1
// MsgInfo( oTagActual:cName )

// Try this way too
// HEval( oTagActual:aAttributes, { | cKey, cValue | _XmlParse( cKey, cValue, oTagActual:cName, @lPass, cSOurce+cFileName, @cEdiLog ) } )

_XmlParse( oTagActual:cName, oTagActual:cData, "", @lPass, cSource+cFileName, @cEdiLog )
endif
end

// Process data...

else

_XmlParse( oTagActual:cName, oTagActual:cData, "", @lPass, cSource+cFileName, @cEdiLog )
IF cDocumentName=="CDMINSHIP"
lDelete := .T.
EXIT
ENDIF
IF cDocumentName=="CDM997"
lDelete := .T.
EXIT
ENDIF

endif
ELSE
EXIT
ENDIF
END
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
 
Posts: 689
Joined: Fri Oct 28, 2005 9:53 am
Location: Houston ∙ Chicago ∙ Los Angeles ∙ Miami ∙ London ∙ Hong Kong

Re: Processing XML Data

Postby Antonio Linares » Wed Mar 13, 2013 3:34 pm

Darrell,

Have you considered to use this ?

FieldPut( FieldPos( oTagActual:cName ), oTagActual:cData )

that would simplify your code very much.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 35 guests