Skip to Content
avatar image
Former Member

Parse HTML to Internal Table

Hi All,

i'm facing a problem in parsing the following generated XLS (HTML):

<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="ProgId" content="Excel.Sheet"/>
<meta name="Generator" content="Microsoft Excel 10"/>
<!--[if !mso]>
<style>
v\\:* {behavior:url(#default#VML);}");
o\\:* {behavior:url(#default#VML);}");
x\\:* {behavior:url(#default#VML);}");
.shape {behavior:url(#default#VML);}");
</style>");
<![endif]-->
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>report</w:Name>
<x:WorksheetOptions>
<x:ProtectContents>False</w:ProtectContents>
<x:ProtectObjects>False</w:ProtectObjects>
<x:ProtectScenarios>False</w:ProtectScenarios>
</w:WorksheetOptions>
</w:ExcelWorksheet>
</w:ExcelWorksheets>
<x:ProtectStructure>False</w:ProtectStructure>
<x:ProtectWindows>False</w:ProtectWindows>
</w:ExcelWorkbook>");
</xml><![endif]-->
<head>
<style>
br {mso-data-placement:same-cell;}
</style>
</head>
<body>
<style>
table {
mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";
}
</style>
<table width="100%">
<tr>
<td align=center colspan=8 valign=top>
<span class="pageHead">
<nobr><h1>Rating</h1></nobr></span>
</td>
</tr>
<tr>
<td align=center colspan=8 valign=top>
<span class="pageHead"><nobr>
Generated Automatically
</nobr></span>
</td></tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>

********************************FROM HERE***********************************
<table border="1" cellspacing="0" cellpadding="0" width="100%">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td style="vnd.ms-excel.numberformat:@">Text 1</td>
<td style="vnd.ms-excel.numberformat:@">Text 2</td>
<td>1</td>
</tr>
<tr>
<td style="vnd.ms-excel.numberformat:@">Text 1</td>
<td style="vnd.ms-excel.numberformat:@">Text 2</td>
<td>2</td>
</tr>
<tr>
<td style="vnd.ms-excel.numberformat:@">Text 1</td>
<td style="vnd.ms-excel.numberformat:@">Text 2</td>
<td>3</td>
</tr>
</table>
********************************TO HERE***********************************

</body></html>

Scenario is the following:

  1. Incoming Email in SAP System
  2. Reading Attachment XLS (XSTRING Format)
  3. Conversion XSTRING to STRING Table
  4. Parse the Result(HTML Format) into Internal Table

I can't convert XSL directly because the file is in XSTRING Format and is not on Local Machine but come from third part software through email.

I want to parse the highlighted part into internal table.

What is the best approach to do this ?

Best regards,

Angelo.

Add comment
10|10000 characters needed characters exceeded

  • Hello,

    What do you mean by "Incoming Email in SAP System" ? Where do you see, in a SAP system, an "incoming email" ? Transaction SBWP ?

  • Former Member Chaouki AKIR

    The transaction to see incoming e-mails is SOIN.

    See this Blog

  • Former Member

    I solved the problem parsing the file with a simple Transformation.

    XML Classes return me a lot of TAG error.

    I separate the relevant part from the internal table

    <table border="1" cellspacing="0" cellpadding="0" width="100%">
    <tr>
    <th>Column 1</th>
    <th>Column 2</th>
    <th>Column 3</th>
    </tr>
    <tr>
    <td style="vnd.ms-excel.numberformat:@">Text 1</td>
    <td style="vnd.ms-excel.numberformat:@">Text 2</td>
    <td>1</td>
    </tr>
    <tr>
    <td style="vnd.ms-excel.numberformat:@">Text 1</td>
    <td style="vnd.ms-excel.numberformat:@">Text 2</td>
    <td>2</td>
    </tr>
    <tr>
    <td style="vnd.ms-excel.numberformat:@">Text 1</td>
    <td style="vnd.ms-excel.numberformat:@">Text 2</td>
    <td>3</td>
    </tr>
    </table>

    ...and use this simple Transformation:

    <?sap.transform simple?>
    <tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
      <tt:root name="READ" type="ddic:ZSTRUCTURE"/>
      <tt:template>
        <table>
          <tt:skip count="1" name="tr"/>
          <tt:loop ref=".READ">
            <tr>
              <td tt:value-ref="COL1"/>
              <td tt:value-ref="COL2"/>
              <td tt:value-ref="COL3"/>
            </tr>
          </tt:loop>
        </table>
      </tt:template>
    </tt:transform>

    Best regards,

    Angelo.

  • Get RSS Feed

2 Answers

  • Dec 21, 2016 at 03:46 PM

    Oddly enough, this reminds me of one of the most famous answers on Stack Overflow (ie. why you do NOT use regex to parse HTML)....

    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

    Add comment
    10|10000 characters needed characters exceeded

  • Dec 20, 2016 at 04:12 PM

    There are lots of possibilities. E.g., you stay with string processing and you parse the relevant part of the HTML file with the help of regular expressions. Or you treat the XSTRING directly as XML and parse it with methods of the sXML library.

    Add comment
    10|10000 characters needed characters exceeded