ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Looking for the correct way I should be reading a XML file.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looking for the correct way I should be reading a XML file.

    I have the following XML file layout that has several layers to it. Using XML-INTO with %Handler I am able to retreive the data contained within the tag, but I can not retrieve the data prior to the tag. Is there a particular way I should go about reading the file to retrieve all of the data and be able to keep the associations between the parent and child tags. Or should I be using XML-SAX, do to the multiple levels in this file. This is my first time attempting to work with XML through RPG.

    I've tried displaying the xml on this post but the site keeps converting it. I'm not seeing an attachment link to upload it. I can email it if anyone wants or needs to see it.

  • #2
    Re: Looking for the correct way I should be reading a XML file.

    try wrapping the XML in code tags. and if you use the advanced mode post entry you'll see a paper clip button for attachments...
    I'm not anti-social, I just don't like people -Tommy Holden

    Comment


    • #3
      Re: Looking for the correct way I should be reading a XML file.

      I tried doing that Tom and the site just formatted the XML, so only the data was displayed. I also tried the quote tag and got the same result. I'll post with code tag below.

      Code:
      
         9999999999
         
             
             01111A111
             Fund 001
             http://www.kjfjfkjkfj.com
             
                 
      	   0998
      	   
      	       
      	           00000001
      	           20111206
      		   003400230056054567
      		   John
      	 	   
       		   Doe
      		   101 Main St.
      		   
      		   
      		   Boston
      		   MA
      		   99999
      		   US
      	       
      	       
      	          00000002
      	          20111206
      	          004398497300000763
      	          Jane
      	          
      	          Doe
      	          102 Main St.
      	          
      	          
      	          Seattle
      	          WA
      	          99997
      	          US
      	          
      	   
      	
            
            
            
            

      Comment


      • #4
        Re: Looking for the correct way I should be reading a XML file.

        did you try adding it as an attachment as i mentioned above (the attachment button does not appear on the quick reply box, you'll have to click the Go Advanced button)
        I'm not anti-social, I just don't like people -Tommy Holden

        Comment


        • #5
          Re: Looking for the correct way I should be reading a XML file.

          Here is the attachment.
          Attached Files

          Comment


          • #6
            Re: Looking for the correct way I should be reading a XML file.

            Originally posted by DAG0000
            ...but I can not retrieve the data prior to the tag.
            are you referring to the "attribute" within the tag itself? if so then you'll likely be using XML-SAX (there maybe be something in v6.1-v7.1 that allows this in XML-INTO but i haven't really had to tinker with XML...nor have had time to play ) on second thought...looking at the v7.1 documentation XML-INTO does support attributes http://publib.boulder.ibm.com/infoce...034.htm#xmlin4
            I'm not anti-social, I just don't like people -Tommy Holden

            Comment


            • #7
              Re: Looking for the correct way I should be reading a XML file.

              You can do this with Xml-Into. (You can even do this without %handler, although %handler allows you to get away from some of the hard coded array elements, and to handle an infinte number of embedments)

              Are you currently using data structures? If you do, post them and we'll modify them to get them to work.
              Michael Catalani
              IS Director, eCommerce & Web Development
              Acceptance Insurance Corporation
              www.AcceptanceInsurance.com
              www.ProvatoSys.com

              Comment


              • #8
                Re: Looking for the correct way I should be reading a XML file.

                No, I mean all the data above the Account Tag. Using XML-INTO with the path set to the Account tag I can get the accounts, but I need the corresponding Work Order, Prospectus and Broker for that Account, which I can not retrieve. If I set the path to root tag I can get all the data but only the first account, I believe this is due to their being only 1 work order in the test data I am using.

                Work order - Could be Multiple Work Orders
                Prospectus - Multiple per Workorder
                Brokers - Multiple per Prospectus
                Accounts - Multiple per Broker

                I suspect that this is too complex for XML-INTO and I should use XML-SAX. Can it be done with XML-INTO?

                Comment


                • #9
                  Re: Looking for the correct way I should be reading a XML file.

                  XML-INTO would work fine you'll be using nested DS to accomplish it but it shouldn't be that hard...
                  I'm not anti-social, I just don't like people -Tommy Holden

                  Comment


                  • #10
                    Re: Looking for the correct way I should be reading a XML file.

                    This is the data structure I am using.

                    Code:
                    D PSPWorkOrders   DS                  Qualified Inz       
                    D  WONbr                        23A                       
                    D  Prospectuses                       LikeDS(Prospectuses)
                                                                              
                    D Prospectuses    DS                  Qualified           
                    D  count                         9A                       
                    D  Prospectus                         LikeDS(Prospectus)  
                                                                              
                    D Prospectus      DS                  Qualified Inz       
                    D  sequence                      9A                       
                    D  CUSIP                        12A                       
                    D  MutualFundName...                                      
                    D                              105A   Varying             
                    D  URL                         305A   Varying             
                    D  Brokers                            LikeDS(Brokers)     
                                                                              
                    D Brokers         DS                  Qualified Inz       
                    D  count                         9A                       
                    D  Broker                             LikeDS(Broker)      
                    D Broker          DS                  Qualified Inz    
                    D  sequence                      9A                    
                    D  DTCCcode                      9A                    
                    D  Accounts                           LikeDS(Accounts) 
                                                                           
                    D Accounts        DS                  Qualified Inz    
                    D  count                         9A                    
                    D  Account                            LikeDS(Account)  
                                                                           
                    D Account         DS                  Qualified Inz    
                    D  sequence                      9A                    
                    D  AccountNumber                35A      
                    D  TradeDate                    12A                    
                    D  ShareholderID                23A                    
                    D  FirstName                   105A                    
                    D  MiddleName                   55A                    
                    D  LastName                    105A                    
                    D  Address1                    305A                    
                    D  Address2                    305A                    
                    D  Address3                    305A                    
                    D  City                         55A      
                    D  State                         7A      
                    D  Zip                          16A      
                    D  Country                       7A

                    Comment


                    • #11
                      Re: Looking for the correct way I should be reading a XML file.

                      Here is a sample program that will support any number of prospectuses, brokers and accounts in any structure and where the only limit is a 2GB max file size. It also runs at V5R2M0 and up.

                      I have built it based on your predefined data structures and XML sample file but I have removed the dependencies so data from prospectus and broker just will hold current data when processing an account.

                      Mapping doesn?t have to be made into initial data structures, which then has to be moved into internal fields, it can be made directly into the internal fields.

                      The problem in XML-INTO is actually the dependency of data structures in complex large XML files because you have to dimension each data structure causing each high level dimensioning to multiply the total size of lower level dimension by the number of dimensions.

                      This force you to make a choice- if you have lots of brokers you may have to limit the number of accounts per broker or vice versa.

                      In the sample there is no need for complex coding of logic because the collecting and processing is straight forward and sequential and there is either no need to define not used structures or elements in order to read the needed data from the XML.

                      Code:
                            /copy qsrc,pxapihdr      General H-Spec's
                      
                            * powerEXT Service Program Connector
                            /copy qsrc,pxapicgicn    Basic HTTP connecter & Productivity Services
                      
                           D PSPWorkOrders   DS                  Qualified Inz
                           D  WONbr                        23A
                      
                           D Prospectuses    DS                  Qualified
                           D  count                         9A
                      
                           D Prospectus      DS                  Qualified Inz
                           D  sequence                      9A
                           D  CUSIP                        12A
                           D  MutualFundName...
                           D                              105A   Varying
                           D  URL                         305A   Varying
                      
                           D Brokers         DS                  Qualified Inz
                           D  count                         9A
                      
                           D Broker          DS                  Qualified Inz
                           D  sequence                      9A
                           D  DTCCcode                      9A
                      
                           D Accounts        DS                  Qualified Inz
                           D  count                         9A
                         
                           D Account         DS                  Qualified Inz
                           D  sequence                      9A
                           D  AccountNumber                35A
                           D  TradeDate                    12A
                           D  ShareholderID                23A
                           D  FirstName                   105A
                           D  MiddleName                   55A
                           D  LastName                    105A
                           D  Address1                    305A
                           D  Address2                    305A
                           D  Address3                    305A
                           D  City                         55A
                           D  State                         7A
                           D  Zip                          16A
                           D  Country                       7A
                      
                            * Internal Fields
                           d acct            s             35a
                           d acctnbr         s             15p 0
                            /free
                      
                             // Initialize the xmlReader
                             clearSrvPgm();
                             xmlFromStmf('/xmlsample.xml');
                             xmlReaderInz(xmlAddr:xmlSize);
                             xmlReaderCase('L');
                      
                             // Read the XML
                             dow xmlReader = 0;
                               select;
                      
                                 // Group collecting - prospectus
                                 when xmlGetNode = '%propexctus';
                                   clear prospectus;
                                 when xmlGetNode = 'prospectus' and xmlGetAttr = 'sequence';
                                   prospectus.sequence = xmlGetData;
                                 when xmlGetNode = 'cusip' and xmlGetAttr = '';
                                   prospectus.cusip = xmlGetData;
                                 when xmlGetNode = 'mutualfundname' and xmlGetAttr = '';
                                   prospectus.mutualfundname = xmlGetData;
                                 when xmlGetNode = 'url' and xmlGetAttr = '';
                                   prospectus.url = xmlGetData;
                      
                                 // Group collecting - brokers
                                 when xmlGetNode = '%brokers';
                                   clear brokers;
                                 when xmlGetNode = 'brokers' and xmlGetAttr = 'count';
                                   brokers.count = xmlGetData;
                      
                                 // Group collecting - broker
                                 when xmlGetNode = '%broker';
                                   clear broker;
                                 when xmlGetNode = 'broker' and xmlGetAttr = 'sequence';
                                   broker.sequence = xmlGetData;
                                 when xmlGetNode = 'dtcccode' and xmlGetAttr = '';
                                   broker.dtcccode = xmlGetData;
                      
                                 // Group collecting - accounts
                                 when xmlGetNode = '%accounts';
                                   clear accounts;
                                 when xmlGetNode = 'accounts' and xmlGetAttr = 'count';
                                   accounts.count = xmlGetData;
                      
                                 // Group collecting - account
                                 when xmlGetNode = '%account';
                                   clear account;
                                 when xmlGetNode = 'account' and xmlGetAttr = 'sequence';
                                   account.sequence = xmlGetData;
                                 when xmlGetNode = 'accountnumber' and xmlGetAttr = '';
                                   account.accountnumber = xmlGetData;
                                    // or map the data directly to local fields
                                   acct = xmlGetData;
                                    // or local numeric fields
                                   acctnbr = xmlGetDataNum;
                      
                                 // ... more field mapping
                      
                                 // Advanced Collecting with partial Xpath check
                                 when xmlGetNode = 'shareholderid' and xmlGetAttr = ''
                                  and xmlGetxpath(2:1) = '/accounts/account';
                                   account.shareholderid = xmlGetData;
                      
                      
                                 // Group Processing - account
                                 when xmlGetNode = '/account';
                                   dsply 'Processing Account';
                                   dsply acct;
                                   dsply account.shareholderid;
                                   dsply broker.dtcccode;
                      
                               endsl;
                             enddo;
                      
                             // Free XML storage
                             xmlFree();
                      
                             *inlr = *on;
                            /end-free

                      Comment


                      • #12
                        Re: Looking for the correct way I should be reading a XML file.

                        but Henrik's example is only valid IF you have powerEXT. so if you don't have, want or can install it then his example is totally useless to you.
                        I'm not anti-social, I just don't like people -Tommy Holden

                        Comment


                        • #13
                          Re: Looking for the correct way I should be reading a XML file.

                          Originally posted by Henrik Rutzou View Post
                          The problem in XML-INTO is actually the dependency of data structures in complex large XML files because you have to dimension each data structure causing each high level dimensioning to multiply the total size of lower level dimension by the number of dimensions.
                          Thats true if you "dimension" your data structures ( Use arrays ) and let xml-into fill the entire xml structure in one fell swoop. If you use the %handler bif on xml-into, you dont have to dimension the data structures. You can retrieve the data one complete fill at a time.

                          Using arrays is handy when the known set of arrays are known to be few, as the single line xml-into can fill out everything in the one lince of code.

                          For xml files which have an unknown number of nested elements, or which has an array structure based upon an array structure based upon another array structure, etc (where the 16MB limit can quickly be reached), then using the %handler bif on xml-into is the way to go.

                          IBM has improved xml-into since it was first released. It's become a poweful bif in of itself. I'm not knocking yours or anyone elses solution. Some xml can be so complex that xml-sax or another third party solution may be the best fit. But for this stuff, its hard to beat xml-into.
                          Michael Catalani
                          IS Director, eCommerce & Web Development
                          Acceptance Insurance Corporation
                          www.AcceptanceInsurance.com
                          www.ProvatoSys.com

                          Comment


                          • #14
                            Re: Looking for the correct way I should be reading a XML file.

                            Tom,

                            that is correct, but powerEXT Core are MIT licensed, build in RPGLE/free, Open Source and completly free

                            Comment


                            • #15
                              Re: Looking for the correct way I should be reading a XML file.

                              Michael ...

                              XML Layout:
                              look at the requirements:

                              Work order - Could be Multiple Work Orders
                              Prospectus - Multiple per Workorder
                              Brokers - Multiple per Prospectus
                              Accounts - Multiple per Broker

                              Comment

                              Working...
                              X