Как извлечь стиль CSS с помощью JSOUP - PullRequest
0 голосов
/ 03 октября 2019

У меня есть html, который приходит из системного буфера обмена при копировании данных в MS Excel,

Я хочу извлечь данные со стилем. Здесь html-контент содержит тег css в STYLE, как показано ниже

xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 15">
<link id=Main-File rel=Main-File
href="file:////Users/tikeshwar-1410/Library/Group%20Containers/UBF8T346G9.Office/TemporaryItems/msohtmlclip/clip.htm">
<link rel=File-List
href="file:////Users/tikeshwar-1410/Library/Group%20Containers/UBF8T346G9.Office/TemporaryItems/msohtmlclip/clip_filelist.xml">
<style>
<!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
@page
    {margin:.75in .7in .75in .7in;
    mso-header-margin:.3in;
    mso-footer-margin:.3in;}
tr
    {mso-height-source:auto;}
col
    {mso-width-source:auto;}
br
    {mso-data-placement:same-cell;}
td
    {padding-top:1px;
    padding-right:1px;
    padding-left:1px;
    mso-ignore:padding;
    color:black;
    font-size:12.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:Calibri, sans-serif;
    mso-font-charset:0;
    mso-number-format:General;
    text-align:general;
    vertical-align:bottom; 
    border:none;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-protection:locked visible;
    white-space:nowrap;
    mso-rotate:0;}
.xl63
    {font-weight:700;}
.xl64
    {background:yellow;
    mso-pattern:black none;}
.xl65
    {color:red;}
.xl66
    {text-decoration:underline;
    text-underline-style:single;}
-->
</style>
</head>

<body link="#0563C1" vlink="#954F72">

<table border=0 cellpadding=0 cellspacing=0 width=174 style='border-collapse:
 collapse;width:130pt'>
<!--StartFragment-->
 <col width=87 span=2 style='width:65pt'>
 <tr height=21 style='height:16.0pt'>
  <td height=21 class=xl63 width=87 style='height:16.0pt;width:65pt'>a</td>
  <td class=xl64 align=right width=87 style='width:65pt'>1</td>
 </tr>
 <tr height=21 style='height:16.0pt'>
  <td height=21 class=xl65 style='height:16.0pt'>b</td>
  <td class=xl66 align=right>2</td>
 </tr>
<!--EndFragment-->
</table>

</body>

</html>

Я могу получить встроенный стиль для элемента y с помощью селектора стиля, но здесь я не могу получить стиль как его внутренний тег.

Есть ли способ получить стиль для каждого TD, TR, а также стиль специального класса? Заранее спасибо

1 Ответ

0 голосов
/ 09 октября 2019

Внутренний CSS описывается как стиль атрибут , а не элемент как встроенный CSS. Таким образом, вы должны получить внутренний CSS с методом attr (). Например:

Document htmlFile = Jsoup.parse(html);
Element firstTableElem = htmlFile.select("table").first();
String tableStyleValue = firstTableElem.attr("style"); //gives you internal css
...