ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

XML question

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

  • XML question

    I have an XML file in IFS. It is wrapped. Is there any way to unwrap that file to a physical file?

    Your help is appreciated

    Thanks
    Nanda

  • #2
    Re: XML question

    It would probably help for you to post a sample of the file. (Because all xml files arent created equally, or correctly)

    It could be accomplished as easily as reading the file, using the xml-into opcode, and havng the results fed into a data structure array based upon the file. Then you could write each ds array element to the file.

    But if the file is built with poor form, then that changes everything.
    Michael Catalani
    IS Director, eCommerce & Web Development
    Acceptance Insurance Corporation
    www.AcceptanceInsurance.com
    www.ProvatoSys.com

    Comment


    • #3
      Re: XML question

      Thank You Michael for your respsonse.

      For example , I have the following XML file
      Code:
      <?xml version="1.0" encoding="UTF-8"?><orders vendor="ABC" version="1.1">
      <order  refnum="1234567" tax="0.0" total="50.0"><date>2011-03-20</date>
      <address><name>Patricia DiMascio</name><street1>6 Jamie Drive</street1>
      <street2/><street3/><city>Johnston</city>
      <state>RI</state><zip>02919</zip>
      <country>United States of America (U.S.A)</country><email/>
      <phone>(401)-942-2604</phone></address></order>
      <order  refnum="1234568" tax="2.86" total="52.86">......
      What I am trying to do is to move the contents of this XML file to a single field physical file of length 132 bytes and then import it into our EDI translator Trusted link which would then populate the target database files based on the mapping. Issue I am having is that One XML contains multiple orders and I want to split them so when it is imported to the translator , I get spearate document in the translator for each order.So I am wondering is there any way to read the above XML file and populate the single field physical file like below

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      "?><orders vendor="ABC" version="1.1">
      <order  refnum="1234567" tax="0.0" total="50.0">
      "><date>2011-03-20</date>
      i.e I want each XML tag in a separate line.
      Is it possible? If this can be done then I can read this file and scan for </order> and then would split orders into separate members of a physical file then import them into EDI translator which would translate as a separate orders.

      Thank You

      Regards
      Nanda

      Comment


      • #4
        Re: XML question

        Originally posted by gudlas2011 View Post
        Thank You Michael for your respsonse.

        For example , I have the following XML file

        <?xml version="1.0" encoding="UTF-8"?><orders vendor="ABC" version="1.1"><order refnum="1234567" tax="0.0" total="50.0"><date>2011-03-20</date><address><name>Patricia DiMascio</name><street1>6 Jamie Drive</street1><street2/><street3/><city>Johnston</city><state>RI</state><zip>02919</zip><country>United States of America (U.S.A)</country><email/><phone>(401)-942-2604</phone></address></order><order refnum="1234568" tax="2.86" total="52.86">......

        What I am trying to do is to move the contents of this XML file to a single field physical file of length 132 bytes and then import it into our EDI translator Trusted link which would then populate the target database files based on the mapping. Issue I am having is that One XML contains multiple orders and I want to split them so when it is imported to the translator , I get spearate document in the translator for each order.So I am wondering is there any way to read the above XML file and populate the single field physical file like below

        <?xml version="1.0" encoding="UTF-8"?>
        "?><orders vendor="ABC" version="1.1">
        <order refnum="1234567" tax="0.0" total="50.0">
        "><date>2011-03-20</date>

        i.e I want each XML tag in a separate line.
        Is it possible? If this can be done then I can read this file and scan for </order> and then would split orders into separate members of a physical file then import them into EDI translator which would translate as a separate orders.

        Thank You

        Regards
        Nanda
        The combining of orders under each vendor makes it a little more difficult to pull off, but its still do-able. I'll look at creating the sample data structures today if I have a little time. I really hate when the xml is unecessarily formed out of a mix of attributes and elements. (ie the single date element is formed right after a series of order attributes, when it could have been an aatirbute itself) It just makes the thing harder to read, and makes you think that the date element may be something special. (In actuality, there's nothing you can do with xml documents you receive, but I just hate to see them done weirdly or inconsistently.)

        How many orders do you typically receive at a time in an xml document?
        Michael Catalani
        IS Director, eCommerce & Web Development
        Acceptance Insurance Corporation
        www.AcceptanceInsurance.com
        www.ProvatoSys.com

        Comment


        • #5
          Re: XML question

          Originally posted by MichaelCatalani View Post
          The combining of orders under each vendor makes it a little more difficult to pull off, but its still do-able. I'll look at creating the sample data structures today if I have a little time. I really hate when the xml is unecessarily formed out of a mix of attributes and elements. (ie the single date element is formed right after a series of order attributes, when it could have been an aatirbute itself) It just makes the thing harder to read, and makes you think that the date element may be something special. (In actuality, there's nothing you can do with xml documents you receive, but I just hate to see them done weirdly or inconsistently.)

          How many orders do you typically receive at a time in an xml document?
          Thanks Michael.

          Few hundred orders in a file.

          Regards
          Nanda

          Comment


          • #6
            Re: XML question

            Yea, a few hundred will be fine.

            Here's how the document would look if it had some formatting. It helps in readability, and in creating a data structure to extract the data into. The bolded tags are where I included a start and end tag for readability.


            Code:
            <?xml version="1.0" encoding="UTF-8"?>
              <orders vendor="ABC" version="1.1">
                <order refnum="1234567" tax="0.0" total="50.0">
                  <date>2011-03-20</date>
                  <address>
                    <name>Patricia DiMascio</name>
                    <street1>6 Jamie Drive</street1>
                    [I][B]<street2>[/B][/I]</street2>
                    [I][B]<street3>[/B][/I]</street3>
                    <city>Johnston</city>
                    <state>RI</state>
                    <zip>02919</zip>
                    <country>United States of America (U.S.A)</country>
                    [I][B]<email>[/B][/I]</email>
                    <phone>(401)-942-2604</phone>
                  </address>
                </order>
              </orders>
            Last edited by MichaelCatalani; September 25, 2011, 01:09 PM.
            Michael Catalani
            IS Director, eCommerce & Web Development
            Acceptance Insurance Corporation
            www.AcceptanceInsurance.com
            www.ProvatoSys.com

            Comment


            • #7
              Re: XML question

              Thanks Michael

              Do you have the code for it?

              Comment


              • #8
                Re: XML question

                Here's the code. I've added a couple of orders to this so you can see how it works. If you put the program in debug mode, and display the XmlDS data structure, you will see how the data is planted in there.

                The actual program performing the work is very small. Most of this code is the xml data. You would simply read the data fro the IFS file into the XmlData field. I included some raw data in the program so you could just copy the program, compile it, and see it work.

                This was a quickie, so there could (and should) be some bugs, but it works.

                Code:
                                                                                            
                d Options         s            256a   Varying                               
                d XmlData         s           1024a   Varying                               
                d X               s             10i 0                                       
                d Y               s             10i 0                                       
                                                                                            
                d XmlDS           ds                  Qualified Inz Dim( 100 )              
                d  Vendor                       20a                                         
                d  Version                       5a                                         
                d  Order                              LikeDS( OrderDS ) Dim( 100 )          
                                                                                            
                d OrderDS         ds                  Qualified Inz                         
                d  RefNum                        7p 0                                       
                d  Tax                           9p 2                                       
                d  Total                         9p 2                                       
                d  Address                            LikeDS( AddressDS )                   
                                                                                            
                d AddressDS       ds                  Qualified Inz                         
                d  Name                         20a                                         
                d  Street1                      20a                                         
                d  Street2                      20a                                         
                d  Street3                      20a                                         
                d  City                         20a                                         
                d  State                         2a                          
                d  Zip                          10a                          
                d  Country                      20a                          
                d  Email                        50a                          
                d  Phone                        20a                          
                                                                             
                 /free                                                       
                                                                             
                    options = 'case=any +                                    
                               path= orders +                                
                               allowextra=yes +                              
                               allowmissing=yes';                            
                                                                             
                   XmlData =                                                 
                   '<?xml version="1.0" encoding="UTF-8"?>+                  
                     <orders vendor="ABC" version="1.1">+                    
                       <order refnum="1234567" tax="0.0" total="50.0">+      
                         <date>2011-03-20</date>+                            
                         <address>+                                          
                           <name>Patricia DiMascio</name>+                   
                           <street1>6 Jamie Drive</street1>+                 
                           <street2/>+                                       
                           <street3/>+                                       
                           <city>Johnston</city>+                            
                           <state>RI</state>+                                       
                           <zip>02919</zip>+                                        
                           <country>United States of America (U.S.A)</country>+     
                           <email/>+                                                
                           <phone>(401)-942-2604</phone>+                           
                         </address>+                                                
                       </order>+                                                    
                       <order refnum="5555999" tax="13.79" total="155.99">+         
                         <date>2011-03-30</date>+                                   
                         <address>+                                                 
                           <name>Michael Catalani</name>+                           
                           <street1>6050 Poplar Ave</street1>+                      
                           <street2/>+                                              
                           <street3/>+                                              
                           <city>Memphis</city>+                                    
                           <state>TN</state>+                                       
                           <zip>38119</zip>+                                        
                           <country>United States of America (U.S.A)</country>+     
                           <email/>+                                                
                           <phone>(901)-672-7572</phone>+                           
                         </address>+                                                
                       </order>+                                                    
                     </orders>';        
                                                            
                   xml-into XmlDS %xml( XmlData : options );                   
                                                                               
                   x=1;                                                        
                   y=1;                                                        
                   dow XmlDS( x ).Vendor <> *blanks;                           
                     dsply XmlDS( x ).Vendor;                                  
                     dow XmlDS( x ).Order( y ).refnum <> 0;                    
                       dsply XmlDS( x ).Order( y ).RefNum;                     
                       y += 1;                                                 
                     enddo;                                                    
                     y=1;                                                      
                     x += 1;                                                   
                   enddo;                                                      
                                                                               
                   *inlr = *on;
                Michael Catalani
                IS Director, eCommerce & Web Development
                Acceptance Insurance Corporation
                www.AcceptanceInsurance.com
                www.ProvatoSys.com

                Comment


                • #9
                  Re: XML question

                  Originally posted by MichaelCatalani View Post
                  Here's the code. I've added a couple of orders to this so you can see how it works. If you put the program in debug mode, and display the XmlDS data structure, you will see how the data is planted in there.

                  The actual program performing the work is very small. Most of this code is the xml data. You would simply read the data fro the IFS file into the XmlData field. I included some raw data in the program so you could just copy the program, compile it, and see it work.

                  This was a quickie, so there could (and should) be some bugs, but it works.

                  Code:
                                                                                              
                  d Options         s            256a   Varying                               
                  d XmlData         s           1024a   Varying                               
                  d X               s             10i 0                                       
                  d Y               s             10i 0                                       
                                                                                              
                  d XmlDS           ds                  Qualified Inz Dim( 100 )              
                  d  Vendor                       20a                                         
                  d  Version                       5a                                         
                  d  Order                              LikeDS( OrderDS ) Dim( 100 )          
                                                                                              
                  d OrderDS         ds                  Qualified Inz                         
                  d  RefNum                        7p 0                                       
                  d  Tax                           9p 2                                       
                  d  Total                         9p 2                                       
                  d  Address                            LikeDS( AddressDS )                   
                                                                                              
                  d AddressDS       ds                  Qualified Inz                         
                  d  Name                         20a                                         
                  d  Street1                      20a                                         
                  d  Street2                      20a                                         
                  d  Street3                      20a                                         
                  d  City                         20a                                         
                  d  State                         2a                          
                  d  Zip                          10a                          
                  d  Country                      20a                          
                  d  Email                        50a                          
                  d  Phone                        20a                          
                                                                               
                   /free                                                       
                                                                               
                      options = 'case=any +                                    
                                 path= orders +                                
                                 allowextra=yes +                              
                                 allowmissing=yes';                            
                                                                               
                     XmlData =                                                 
                     '<?xml version="1.0" encoding="UTF-8"?>+                  
                       <orders vendor="ABC" version="1.1">+                    
                         <order refnum="1234567" tax="0.0" total="50.0">+      
                           <date>2011-03-20</date>+                            
                           <address>+                                          
                             <name>Patricia DiMascio</name>+                   
                             <street1>6 Jamie Drive</street1>+                 
                             <street2/>+                                       
                             <street3/>+                                       
                             <city>Johnston</city>+                            
                             <state>RI</state>+                                       
                             <zip>02919</zip>+                                        
                             <country>United States of America (U.S.A)</country>+     
                             <email/>+                                                
                             <phone>(401)-942-2604</phone>+                           
                           </address>+                                                
                         </order>+                                                    
                         <order refnum="5555999" tax="13.79" total="155.99">+         
                           <date>2011-03-30</date>+                                   
                           <address>+                                                 
                             <name>Michael Catalani</name>+                           
                             <street1>6050 Poplar Ave</street1>+                      
                             <street2/>+                                              
                             <street3/>+                                              
                             <city>Memphis</city>+                                    
                             <state>TN</state>+                                       
                             <zip>38119</zip>+                                        
                             <country>United States of America (U.S.A)</country>+     
                             <email/>+                                                
                             <phone>(901)-672-7572</phone>+                           
                           </address>+                                                
                         </order>+                                                    
                       </orders>';        
                                                              
                     xml-into XmlDS %xml( XmlData : options );                   
                                                                                 
                     x=1;                                                        
                     y=1;                                                        
                     dow XmlDS( x ).Vendor <> *blanks;                           
                       dsply XmlDS( x ).Vendor;                                  
                       dow XmlDS( x ).Order( y ).refnum <> 0;                    
                         dsply XmlDS( x ).Order( y ).RefNum;                     
                         y += 1;                                                 
                       enddo;                                                    
                       y=1;                                                      
                       x += 1;                                                   
                     enddo;                                                      
                                                                                 
                     *inlr = *on;

                  Thank you Michael.

                  Regards
                  Nanda

                  Comment


                  • #10
                    Re: XML question

                    @nanda

                    Have you solved the problem ?

                    Comment


                    • #11
                      Re: XML question

                      I have a command that can re-structure any XML file in my Core library.

                      It is open source (free) and can be downloaded at http://powerEXT.com


                      Here is the command run within an RPGLE program:
                      Code:
                      xmlFile = '/order.xml';                          
                      qcmd('PXXMLXMLCM FILEIN(''' + xmlFile + ''') ' + 
                        'FILEOUT(''' + xmlFile + ''')');
                      I have also subprocedures that can split a multi document (orders) XML file into seperate XML files with only one document (order) pr XML file without reading the whole document structure:

                      Code:
                       /copy qsrc,pxapihdr      General H-Spec's                            
                                                                                            
                       * powerEXT Service Program Connector                                 
                       /copy qsrc,pxapicgicn    Basic HTTP connecter & Productivity Services
                                                                                            
                       * Declare Internal Variables                                         
                      d vendor          s             20a   varying                         
                      d version         s             20a   varying                         
                      d refnum          s             20a   varying                         
                                                                                            
                      d store           s             10i 0                                 
                      d xmlFile         s            256a   varying                         
                      d tmpFile         s            256a   varying                         
                                                                                            
                                                                                   
                      // Clear powerEXT Service Program & Responce Object                 
                      clearSrvPgm();                                                      
                      
                      // Initialize the xmlReader                                
                      clearSrvPgm();                                             
                      xmlFromStmf(xmlFile);                                      
                      xmlReaderInz(xmlAddr:xmlSize:*off:*on);                    
                      xmlReaderCase('L');                                        
                                                                                 
                      // Read the XML                                            
                      dow xmlReader = 0;                                         
                        select;                                                  
                                                                                 
                          // save variables                                      
                          when xmlGetNode = 'orders' and xmlGetAttr = 'vendor';  
                            // save vendor                                       
                            vendor = xmlGetData;                                 
                          when xmlGetNode = 'orders' and xmlGetAttr = 'version'; 
                            // save version                                      
                            version = xmlGetData;                                
                          when xmlGetNode = 'order' and xmlGetAttr = 'refnum';   
                            // save order refnum                                 
                            refnum = xmlGetData;                                 
                                                                                               
                          // at start of an order create a duplicate header                    
                          when xmlGetNode = 'order' and xmlGetAttr = '';                       
                            store = storeNew();                                                
                            storeInz(store);                                                   
                            storeAppendText(store:'<?xml version="1.0" encoding="UTF-8"?>');   
                            storeAppendText(store:nl);                                         
                            storeAppendText(store:'<orders vendor="');                         
                            storeAppendText(store:vendor);                                     
                            storeAppendText(store:'" version="');                              
                            storeAppendText(store:version);                                    
                            storeAppendText(store:'">');                                       
                            storeAppendText(store:nl);                                         
                                                                                               
                          // at end of each order append the order to the header and save it to IFS
                          when xmlGetNode = '/order';                                          
                            storeAppend(store:xmlAddrOuter:xmlSizeOuter);                      
                            storeAppendText(store:nl);                                         
                            storeAppendText(store:'</orders>');                                
                            tmpFile = '/order' + refnum + '.xml';                              
                            storeToStmf(store:tmpFile:1208);                                
                            storeFree(store);                                               
                      
                            // copy the STMF order to a PF Member                                      
                            qcmd('ADDPFM FILE(QGPL/XML) ' +                                 
                             'MBR(X' + refnum + ')');                                       
                            qcmd('CPYFRMSTMF FROMSTMF(''' + tmpFile + ''') ' +              
                             'TOMBR(''QSYS.LIB/QGPL.LIB/XML.FILE/X' + refnum + '.MBR'') ' + 
                             'MBROPT(*REPLACE)');                                           
                            qcmd('del (''' + tmpFile + ''')');                              
                      
                        endsl;                                                              
                      enddo;                                                                
                                                                                            
                      // Free XML storage                                                   
                      xmlFree();
                      The result (file/member 1):
                      Code:
                      <?xml version="1.0" encoding="UTF-8"?>                                      
                      <orders vendor="ABC" version="1.1">                                         
                              <order refnum="1234567" tax="0.0" total="50.0">                             
                                      <date>2011-03-20</date>                                     
                                      <address>                                                   
                                              <name>Patricia DiMascio</name>                      
                                              <street1>6 Jamie Drive</street1>                    
                                              <street2 />                                         
                                              <street3 />                                         
                                              <city>Johnston</city>                               
                                              <state>RI</state>                                   
                                              <zip>02919</zip>                                    
                                              <country>United States of America (U.S.A)</country> 
                                              <email />                                           
                                              <phone>(401)-942-2604</phone>                       
                                      </address>                                                  
                              </order>                                                            
                      </orders>
                      File/member 2:
                      Code:
                      <?xml version="1.0" encoding="UTF-8"?>                                      
                      <orders vendor="ABC" version="1.1">                                         
                              <order refnum="5555999" tax="13.79" total="155.99">                         
                                      <date>2011-03-30</date>                                     
                                      <address>                                                   
                                              <name>Michael Catalani</name>                       
                                              <street1>6050 Poplar Ave</street1>                  
                                              <street2 />                                         
                                              <street3 />                                         
                                              <city>Memphis</city>                                
                                              <state>TN</state>                                   
                                              <zip>38119</zip>                                    
                                              <country>United States of America (U.S.A)</country> 
                                              <email />                                           
                                              <phone>(901)-672-7572</phone>                       
                                      </address>                                                  
                              </order>                                                            
                      </orders>

                      Comment


                      • #12
                        Re: XML question

                        Thanks for sharing this is nice.. and the price is perfect.
                        It is open source (free) and can be downloaded at http://powerext.com/
                        All my answers were extracted from the "Big Dummy's Guide to the As400"
                        and I take no responsibility for any of them.

                        www.code400.com

                        Comment

                        Working...
                        X