Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Java提取HTML中两个链接之间的文本_Java_Android_Xml_Parsing_Epub - Fatal编程技术网

通过Java提取HTML中两个链接之间的文本

通过Java提取HTML中两个链接之间的文本,java,android,xml,parsing,epub,Java,Android,Xml,Parsing,Epub,我正在尝试使用Java从ePub文件检索文本数据。ePub文件的文本位于HTML文件中,该文件的格式如下- <h2 id="pgepubid00001">Chapter I</h2> <p>Some text</p> <p>Another line of Text</p> <br/> <h2 id="pgepubid00001">Chapter II</h2> etc.. 第一章

我正在尝试使用Java从ePub文件检索文本数据。ePub文件的文本位于HTML文件中,该文件的格式如下-

<h2 id="pgepubid00001">Chapter I</h2>

<p>Some text</p>
<p>Another line of Text</p>

<br/>

<h2 id="pgepubid00001">Chapter II</h2>

etc..
第一章
一些文本

另一行文字


第二章 等
在打开此文件之前,我已经知道我需要提取的章节id,并且可以找到下一章节的id。因此,我认为一种合乎逻辑的方法是尝试在SAX解析器中解析它,并提取每个段落中的文本,直到到达下一章的链接。但事实证明,这是一项相当艰巨的任务

当然,一切都是动态的,所以没有设置链接等。HTML是半严格格式的,所以我没想到解析会有这么大的问题。有人能推荐一种提取所需文本的好方法吗


解决方案只需要JAVA,不能使用其他语言。我希望在安卓设备上实现这一点,好吧,你们知道章节的ID,为什么不使用String.indexOf

start = text.indexOf("<h2 id=\"pgepubid00001\">");
end = text.indexOf("<h2 id=\"pgepubid00002\">");

whatYoureLookingFor = text.substring(start, end-start)
start=text.indexOf(“”);
end=text.indexOf(“”);
WhatyYourelookingfor=text.substring(开始,结束-开始)

保持简单。

有没有一种直接的方法将HTML取出并放入字符串中?所以您也想删除HTML标记?请尝试String.replaceAll(“]+>”,“”)。无论如何,解析它可能是个好主意。使用jtidy将html转换为有效的xml。实际上,我只是通过InputStream将其加载到字符串中,然后取出html。一旦你给了我这个想法,我就直截了当了!多谢了,老兄,我用这个把头撞到墙上已经有一段时间了,我不知道为什么我以前没有想到:)