0

I need to create HTML table parser, that will read the table cells in the correct order.

Code I have so far:

html := '<body>
           <table border="1">
             <tr>
               <td><b>A1</b></td>
               <td><i>B1</i></td>
             </tr>
             <tr>
               <td><b>A2</b></td>
               <td><i>B2</i></td>
             </tr>
           </table>
         </body>';

FOR r IN (SELECT rownum rn, td FROM xmltable('*/table/tr' passing xmltype(html)
                                                         columns td xmltype path './td'))
LOOP
  FOR c IN (SELECT cell FROM xmltable('.' passing r.td
                                                  columns cell VARCHAR(200) path '.'))
  LOOP
    dbms_output.put_line('Row ' || r.rn || ': ' || c.cell);

  END LOOP;
END LOOP;

Now the result is:

Row 1: A1B1
Row 2: A2B2

What I need is:

Row 1: A1
Row 1: B1
Row 2: A2
Row 2: B2

How can I achieve that? Thanks for responses.

4 Answers 4

3

One query in enough.

SELECT SEQNO,text FROM xmltable('//*/table/tr' passing xmltype('<body>
           <table border="1">
             <tr>
               <td><b>A1</b></td>
               <td><i>B1</i></td>
             </tr>
             <tr>
               <td><b>A2</b></td>
               <td><i>B2</i></td>
             </tr>
           </table>
         </body>')
 columns td xmltype path '.' , "SEQNO" FOR ORDINALITY)
,xmltable('//td' passing td columns text varchar2(100) path './*/text()');
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming the HTML column is a clob, you can do it as below:

declare
html clob:= '<body>
           <table border="1">
             <tr>
               <td><b>A1</b></td>
               <td><i>B1</i></td>
             </tr>
             <tr>
               <td><b>A2</b></td>
               <td><i>B2</i></td>
             </tr>
           </table>
         </body>';
begin
FOR r IN (SELECT rownum rn, td FROM xmltable('*/table/tr' passing xmltype(html)
                                                         columns td xmltype path './td'))
LOOP
  FOR c IN (SELECT cell FROM xmltable('./td'  passing r.td
                                                  columns cell VARCHAR(200) path '.'))
  LOOP
    dbms_output.put_line('Row ' || r.rn || ': ' || c.cell);

  END LOOP;
END LOOP;

end;

Comments

2

This might do the trick :)

    DECLARE
      html VARCHAR2(1000) := '<body>           
         <table border="1">             
           <tr>               
                <td><b>A1</b></td>               
                <td><i>B1</i></td>             
           </tr>             
           <tr>                 
                <td><b>A2</b></td>               
                <td><i>B2</i></td>             
           </tr>           
         </table>         
       </body>';
BEGIN
  FOR r IN
  (SELECT rownum rn,
    td
  FROM xmltable('*/table/tr' passing xmltype(html) columns td xmltype path '.')
  )
  LOOP
    FOR c IN
    (SELECT cell
    FROM xmltable('*/td/.' passing r.td columns cell VARCHAR(200) path '.')
    )
    LOOP
      dbms_output.put_line('Row ' || r.rn || ': ' || c.cell);
    END LOOP;
  END LOOP;
END;

Output: -

 Row 1: A1
 Row 1: B1
 Row 2: A2
 Row 2: B2

Comments

0

enter image description here enter image description here
you can also create beautiful jquery based responsive table (Note : REPORT IS PREPARED WITH ORACLE 11G XE DUMMY DATA OF HR SCHEMA EMPLOYEE TABLE)

        DECLARE
               tbl_nm      VARCHAR2 (100) := 'MRKT';
               schema_nm   VARCHAR2 (100) := 'STDDATA_STAGE';
            
               CURSOR C1
               IS
                    SELECT column_name STR
                      FROM all_tab_cols
                     WHERE table_name = tbl_nm AND OWNER = schema_nm
                  ORDER BY COLUMN_ID;
            
            BEGIN
               DBMS_OUTPUT.put_line ('begin');
            
               DBMS_OUTPUT.put_line (
                  'DBMS_OUTPUT.put_line(''<!DOCTYPE html>
            <html lang="en" >
            
            <head>
            
            <script src="https://cpwebassets.codepen.io/assets/common/stopExecutionOnTimeout-157cd5b220a5c80d4ff8e0e70ac069bffd87a61252088146915e8726e5d9f147.js"></script> 
            <script src=''''https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js''''> </script> 
            <script src=''''https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js''''> </script> 
            <script src=''''https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js''''> </script> 
            <script src=''''https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js''''> </script> 
            <script src=''''https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js''''></script>
            <script src=''''https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js''''> </script>  
            <script src=''''https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js''''> </script> 
            
            <meta charset="UTF-8">
            <link rel="apple-touch-icon" type="image/png" href="https://cpwebassets.codepen.io/assets/favicon/apple-touch-icon-5ae1a0698dcc2402e9712f7d01ed509a57814f994c660df9f7a952f3060705ee.png" />
            <meta name="apple-mobile-web-app-title" content="CodePen">
            
            <link rel="shortcut icon" type="image/x-icon" href="https://cpwebassets.codepen.io/assets/favicon/favicon-aec34940fbc1a6e787974dcd360f2c6b63348d4b1f4e06c77743096d55480f33.ico" />
            
            <link rel="mask-icon" type="" href="https://cpwebassets.codepen.io/assets/favicon/logo-pin-8f3771b1072e3c38bd662872f6b673a722f4b3ca2421637d5596661b4e2132cc.svg" color="#111" />
            
            
              <title>CodePen - HTML Table to Excel, CSV and PDF</title>
              
              
              <link rel=''''stylesheet'''' href=''''https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css''''>
            <link rel=''''stylesheet'''' href=''''https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css''''>
              
            <style>
            //https://datatables.net/extensions/buttons/examples/html5/simple.html
            </style>
            
              <script>
              window.console = window.console || function(t) {};
            </script>
            
              
              
              <script>
              if (document.location.search.match(/type=embed/gi)) {
                window.parent.postMessage("resize", "*");
              }
            </script>
            
            
            </head>
            
            <body translate="no" >
              <table id="example" class="display" cellspacing="0" width="100%">
                    <thead>          
                      <tr>'');');
            
            
               FOR i IN C1
               LOOP
                  DBMS_OUTPUT.put_line (
                        'DBMS_OUTPUT.put_line(''<th>''||'
                     || ''''
                     || I.STR
                     || ''''
                     || '||''</th>'');');
               END LOOP;
            
               DBMS_OUTPUT.put_line ('DBMS_OUTPUT.put_line(''  </tr>
                    </thead><tbody>'');');
            
            
            
               DBMS_OUTPUT.put_line ('for i in (select *from ' || tbl_nm || ')loop
               ');
               DBMS_OUTPUT.put_line ('DBMS_OUTPUT.put_line(''</tr>
               '');');
            
               FOR i IN C1
               LOOP
                  DBMS_OUTPUT.put_line (
                        'DBMS_OUTPUT.put_line(''<td>''||'
                     || 'i.'
                     || I.STR
                     || '||''</td>'');');
               END LOOP;
            
               DBMS_OUTPUT.put_line ('DBMS_OUTPUT.put_line(''</tr>'');');
               DBMS_OUTPUT.put_line ('end loop;');
            
               DBMS_OUTPUT.put_line (
                  'DBMS_OUTPUT.put_line(''  </tbody>
                </table>
              
                  <script id="rendered-js" >
            $(document).ready(function () {
              $(''''#example'''').DataTable({
                dom: ''''Bfrtip'''',
                buttons: [
                ''''copyHtml5'''',
                {
                  extend: ''''excelHtml5'''',
                  title: ''''Project Report - '''' + new Date().toJSON().slice(0, 10).replace(/-/g, ''''-'''') },
            
                ''''csvHtml5'''',
                ''''pdfHtml5''''] });
            
            
            });
            //# sourceURL=pen.js
                </script>
            
              
            
            </body>
            
            </html>'');');
               DBMS_OUTPUT.put_line ('end;');
            END;
    
    
      [1]: https://i.sstatic.net/Zt84f.png

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.