2

我目前正在使用 Jsoup 解析器来获取一些 HTML,然后为特定标签更改一些标题。问题是由于某种原因,当我的 html 通过 jsoup 解析器时,它似乎被改变了。有没有办法告诉 jsoup 不附加任何 html.body 标签,或者不添加缺少的标签?

它似乎正在改变我的桌子。

原版

 <div class="mstrPanelPortrait">
         <table cellpadding="0" class="pane" cellspacing="0">
            <tr>
               <td>
                  <table class="pane" cellspacing="0">
                     <tr>
                        <td class="mstrPanelBody" sty="body">
                           <div>
                              <div class="mstrBrowser">

放入jsoup后

 <div class="mstrPanelPortrait" title="darrensTest">
         <table cellpadding="0" class="pane" cellspacing="0">
            <tbody>
               <tr>
                  <td>
                     <table class="pane" cellspacing="0">
                        <tbody>
                           <tr>
                              <td class="mstrPanelBody" sty="body">

你可以看到 tbody 在几个地方被添加了。不知道为什么

整个 html

<div or="2" class="mstrTransform" cx="[0,1,2,3,4]" id="FolderObjectBrowser_display" ty="editor" cxid="FolderObjectBrowser_display_cmm" rsz="0" dg="0" iframe="true" style="display:block;" name="FolderObjectBrowser_display" scriptclass="mstrReportAllObjectsImpl" ors="3">
   <form id="FolderObjectBrowser_display_form" name="FolderObjectBrowser_display_form" target="frameManager" action="mstrWeb" method="post" onsubmit="appendPageState(this);">
      <input id="iframe" name="iframe" value="true" class="mstrHiddenInput" type="hidden"/>
      <input name="evt" value="5005" class="mstrHiddenInput" type="hidden"/>
      <input name="src" value="mstrWeb.report.5005" class="mstrHiddenInput" type="hidden"/>
      <div class="mstrPanelPortrait">
         <table cellpadding="0" class="pane" cellspacing="0">
            <tr>
               <td>
                  <table class="pane" cellspacing="0">
                     <tr>
                        <td class="mstrPanelBody" sty="body">
                           <div>
                              <div class="mstrBrowser">
                                 <div id="folerBoxContainerID" class="folerBoxContainer">
                                    <select name="oeFolderID" class="mstrAncestors" sty="folderList">
                                       <option selected="1" title="MicroStrategy Tutorial" level="0" value="D43364C684E34A5F9B2F9AD7108F7828">MicroStrategy Tutorial</option>
                                       <option islink="true" title="Data Explorer" level="0" value="37ED6C6202E14C3181F1F4A043A1CAA8">Data Explorer</option>
                                       <option islink="true" title="My Personal Objects" level="0" value="8D67908E11D3E4981000E787EC6DE8A4">My Personal Objects</option>
                                       <option islink="true" title="Attributes" level="0" value="6F55FB47F9974EABA18CB0C5FF46785C">Attributes</option>
                                       <option islink="true" title="Metrics" level="0" value="E0CCB9CF22104A489CBE78D974AFD19E">Metrics</option>
                                       <option islink="true" title="Hierarchies" level="0" value="C2A0BB1ACAAD45A18B8CA8AECF0A35EE">Hierarchies</option>
                                    </select>
                                    <a><img id="upFolder" title="Up One Level" alt="Up One Level" name="upFolder" class="mstrIcon-btn mstrIcon-btnUpFolderDisabled" src="../images/1ptrans.gif"/></a><a target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=83005&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.83005"><img id="changeFormat" title="Tree" alt="Tree" name="changeFormat" class="mstrIcon-btn mstrIcon-btnChangeDisplayFormatTree" src="../images/1ptrans.gif"/></a>
                                 </div>
                                 <div class="mstrSearchDiv"><span id="name_label">Find:</span><input id="searchArg" name="name" value="" class="mstrInputText" onkeydown="return microstrategy.bone('FolderObjectBrowser_display').checkForFormSubmit(arguments[0]);" type="text"/><input id="search" title="Find" alt="Find" name="98002" class="mstrIcon-btn mstrIcon-btnFind" src="../images/1ptrans.gif" border="0" type="image"/></div>
                                 <div style="position:relative">
                                    <div sty="fileList">
                                       <div id="list" class="mstrSmallIconView">
                                          <div title="Folder:  Project Builder; Folder for all the objects created by Project Builder" dss_ty="8"><span class="mstrIcon-lv-f mstrIcon-lv"><span></span></span><a title="Folder for all the objects created by Project Builder" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=42EEDD41A6954F7485453C170AA3F8BE">Project Builder</a></div>
                                          <div title="Folder:  Project Objects" dss_ty="8"><span class="mstrIcon-lv-f mstrIcon-lv"><span></span></span><a title="" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=02C37D85EE25483AA5708E2BFE858B92">Project Objects</a></div>
                                          <div title="Folder:  Public Objects; Folder for all public objects" dss_ty="8"><span class="mstrIcon-lv-f mstrIcon-lv"><span></span></span><a title="Folder for all public objects" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=98FE182C2A10427EACE0CD30B6768258">Public Objects</a></div>
                                          <div title="Folder:  Schema Objects; Folder for all schema objects" dss_ty="8"><span class="mstrIcon-lv-f mstrIcon-lv"><span></span></span><a title="Folder for all schema objects" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=95C3B713318B43D490EE789BE27D298C">Schema Objects</a></div>
                                          <div title="Folder:  Data Explorer; Hierarchy groups folder" dss_ty="8"><span class="mstrIcon-lv mstrIcon-lv-fh"><span class="sc"></span></span><a title="Hierarchy groups folder" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=37ED6C6202E14C3181F1F4A043A1CAA8">Data Explorer</a></div>
                                          <div title="Folder:  My Personal Objects" dss_ty="8"><span class="mstrIcon-lv mstrIcon-lv-fmo"><span class="sc"></span></span><a title="" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=8D67908E11D3E4981000E787EC6DE8A4">My Personal Objects</a></div>
                                          <div title="Folder:  Attributes" dss_ty="8"><span class="mstrIcon-lv mstrIcon-lv-fa"><span class="sc"></span></span><a title="" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=6F55FB47F9974EABA18CB0C5FF46785C">Attributes</a></div>
                                          <div title="Folder:  Metrics" dss_ty="8"><span class="mstrIcon-lv mstrIcon-lv-fm"><span class="sc"></span></span><a title="" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=E0CCB9CF22104A489CBE78D974AFD19E">Metrics</a></div>
                                          <div title="Folder:  Hierarchies" dss_ty="8"><span class="mstrIcon-lv mstrIcon-lv-fh"><span class="sc"></span></span><a title="" target="frameManager" class="mstrLink" onclick="return submitLink(this, event);" href="mstrWeb?iframe=true&evt=98001&src=mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001&oeFolderBlockBegin=1&oeFolderID=C2A0BB1ACAAD45A18B8CA8AECF0A35EE">Hierarchies</a></div>
                                       </div>
                                    </div>
                                 </div>
                                 <table id="FolderObjectBrowser_display_oCount" width="100%" name="FolderObjectBrowser_display_oCount" cellpadding="0" border="0" cellspacing="2">
                                    <tr>
                                       <td align="LEFT">&nbsp;4 item(s) found</td>
                                    </tr>
                                 </table>
                              </div>
                              <input name="evt" value="98001" class="mstrHiddenInput" type="hidden"/><input name="src" value="mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98001" class="mstrHiddenInput" type="hidden"/><input name="evt" value="98002" class="mstrHiddenInput" type="hidden"/><input name="src" value="mstrWeb.report.frame.accordion.tbObjBrwsr.pbt.FolderObjectBrowser.98002" class="mstrHiddenInput" type="hidden"/><input id="evtorder" name="evtorder" value="98001,98002" class="mstrHiddenInput" type="hidden"/>
                           </div>
                        </td>
                     </tr>
                  </table>
               </td>
            </tr>
         </TABLE>
      </div>
      <div class="mstrSpaceAfterEditor"><img title="" height="3" alt="" width="1" src="../images/1ptrans.gif" border="0"/></div>
   </form>
</div>
4

1 回答 1

1

据我所知,没有办法告诉 Jsoup 不平衡标签。

您可以改用非默认 XML 解析器,它不会添加任何新标签(例如tbody),而只会平衡尚未平衡的标签。

那么,如果 Jsoup 不添加任何标签,而只是平衡 HTML 是否可以呢?

如果该问题的答案是肯定的,那么您应该使用 XML 解析器而不是默认的 HTML 解析器。

doc = Jsoup.parse(html, "", Parser.xmlParser());

这将解析 HTML,但会添加结束标签,但不会添加不存在的标签,因此不会改变结构。然后,您可以以正常的 Jsoup 方式从文档中进行选择。

<div class="mstrPanelPortrait"> 
 <table cellpadding="0" class="pane" cellspacing="0"> 
  <tr> 
   <td> 
    <table class="pane" cellspacing="0"> 
     <tr> 
      <td class="mstrPanelBody" sty="body"> 
       <div> 
        <div class="mstrBrowser"></div>
       </div></td>
     </tr>
    </table></td>
  </tr>
 </table>
</div>  
于 2013-10-07T21:13:21.637 回答