我目前正在使用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"> 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>
答案 0 :(得分: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>