[英]Parse elements from a html file (treating it as a text file) in java
我有html文件。 我正在尝试提取两个锚点之间的“表”内容。
这是示例html内容:
<HTML>
<HEAD>
<TITLE>
Test Doc
</TITLE>
</HEAD>
<BODY LINK=#000000 VLINK=#000000 ALINK=#990000>
<A NAME = "linkTab0000"></A>
<TABLE CELLPADING=1 BORDER=2 WIDTH=100%>
<TR>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#a6caf0>
<B><FONT SIZE=3 COLOR=#000000 FACE='HP Simplified'>Test Entity</FONT></B></TD>
</TR>
</TABLE><TABLE CELLPADING=2 BORDER=2 WIDTH=100%>
<TR>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Name</FONT></B></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Datatype</FONT></B></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Definition</FONT></B></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Note</FONT></B></TD>
</TR>
<TR>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>test</FONT></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>test</FONT></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>test</FONT></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'> </FONT></TD>
</TR>
</TABLE>
<A NAME = "linkTab0001"></A>
<TABLE CELLPADING=1 BORDER=2 WIDTH=100%>
<TR>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#a6caf0>
<B><FONT SIZE=3 COLOR=#000000 FACE='HP Simplified'>Test Entity</FONT></B></TD>
</TR>
</TABLE><TABLE CELLPADING=2 BORDER=2 WIDTH=100%>
<TR>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Name</FONT></B></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Datatype</FONT></B></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Definition</FONT></B></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#64b1ff WIDTH=200>
<B><FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>Note</FONT></B></TD>
</TR>
<TR>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>test</FONT></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>CHAR(18)</FONT></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'>test</FONT></TD>
<TD ALIGN=LEFT VALIGN=TOP BGCOLOR=#ffffff>
<FONT SIZE=2 COLOR=#000000 FACE='HP Simplified'> </FONT></TD>
</TR>
</TABLE>
我想在“ <A NAME = "linkTAB.....
”元素之间提取“ TABLE”元素。
以下是我正在使用的代码:
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
System.out.println(doc.html());
String inputStr = doc.html();
String link = "<a name = "+"linkTab";
Pattern p = Pattern.compile("(\\b^"+link+"\\b)(.*?)(\\^"+link+"\\b)");
Matcher m = p.matcher(inputStr);
List<String> matches = new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}
我也尝试使用bufferedreader,但它忽略了String link =“
请让我知道任何建议。
谢谢
正则表达式不是解析此类XML文件的好方法。
您最好使用XPath或类似Inbuild CSS的选择器。
这是我为您解决的问题的解决方法:
public static void main(String[] args) throws IOException {
// Read your html into a string
StringWriter writer = new StringWriter();
IOUtils.copy(Main.class.getResourceAsStream("/so18644171/html.html"), writer);
String theString = writer.toString();
Document doc = Jsoup.parse(theString);
// a[name^=linkTab] means:
// all a's having a attribute name, starting with "linkTab"
Elements linkTabs = doc.select("a[name^=linkTab] + table");
// "a[name^=linkTab] + table means: All tables followed by a[...]
System.out.println(linkTabs);
}
打印:
<table cellpading="1" border="2" width="100%">
<tbody>
<tr>
<td align="LEFT" valign="TOP" bgcolor="#a6caf0"> <b><font size="3" color="#000000" face="HP Simplified">Test Entity</font></b></td>
</tr>
</tbody>
</table>
<table cellpading="1" border="2" width="100%">
<tbody>
<tr>
<td align="LEFT" valign="TOP" bgcolor="#a6caf0"> <b><font size="3" color="#000000" face="HP Simplified">Test Entity</font></b></td>
</tr>
</tbody>
</table>
我已将此示例上传到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.