we have this HTML document. (HTML received from the first site. The sites will be different)

<HTML xmlns:OG="http://opengraphprotocol.org/schema/"xmlns:FB="http://www.facebook.com/2008/fbml"> <style type="text/css"></style> <BODY> <DIV class="block0"> <DIV class="pad0"> <DIV class="fblock"> <DIV class="block61"> <DIV class="rpad"><H1 class="title">ЗАГОЛОВОК СТАТЬИ</H1> <SPAN class="dt2">Понедельник, 26 сентября 2016, 16:08</SPAN> <DIV class="text"> <P>Во французском городе Ницца по подозрению в планировании теракта были задержаны двое девушек.</P> <P>Об этом сообщает <A href="https://www.afp.com">AFP</A>.</P> <P>Девушки в возрасте 17 и 18 лет подозреваются в планировании террористического нападения под руководством французского джихадиста Рашида Кассима.</P> <P>Подростки рассказали следователям, что они планировали нападение под влиянием Рашида Кассима, который сейчас наход ится в Сирии или Ираке, на территории, которая контролируется террористической организацией &quot;Исламское государство&quot;.</P> <P>Рашид Кассим, по мнению следователей, играет роль подстрекателя или спонсора, фактически призывая потенциальных террористов к совершению джихада во Франции.</P><P>Стоит добавить, что во время обыска в помещениях, которые принадлежат задержанными, стражи порядка не нашли оружия.</P> <P>Издание отмечает, что девушки живут в том же районе, что и <A href="/rus/news/2016/07/15/7052184/">Мохамед Лахуайедж-Бухлель, убивший 86 человек</A> в Ницце 14 июля.</P> <P>Напомним, недавно французская <A href="/rus/news/2016/09/14/7054550/">полиция задержала подростка</A>, подозреваемого в подготовке теракта в Париже.</P> </DIV> <DIV class="btit4"><SPAN>ЧИТАЙ ТАКЖЕ</SPAN> </DIV> </DIV> </DIV> </DIV> </DIV> </DIV> </BODY> </HTML> 

I need to get let to the header. I tried to write this HTML into a document and do:

 String path = htmldoc.substring(htmldoc.indexOf("<html"),htmldoc.indexOf("title") +7); 

but this does not work on all sites and the path after the title just cuts off. and I need to get this result -

 <HTML xmlns:OG="http://opengraphprotocol.org/schema/" xmlns:FB="http://www.facebook.com/2008/fbml"> <BODY> <DIV class="block0"> <DIV class="pad0"> <DIV class="fblock"> <DIV class="block61"> <DIV class="rpad"> <H1 class="title">ЗАГОЛОВОК СТАТЬИ</H1> </DIV> </DIV> </DIV> </DIV> </DIV> </BODY> </HTML> 

and the same path to the content of the article.)

maybe there is some kind of library that allows you to do this?

    1 answer 1

    About type Jsoup it becomes in one line so:

     String html = ...; System.out.println(Jsoup.parse(html).getElementsByClass("title").get(0).text()); 

    You can get the path to the found element using the parent() method somehow (pseudocode, did not check, but the idea is this):

     String path = ""; Element target = Jsoup.parse(html).getElementsByClass("title").get(0); while(target!=null){ path = path.prepend(target.getName()); target = target.getParent(); } 
    • Yes. get the title is elementary. but I need a path to the header as in the sample question <html> <body> <div> <div> header <div /> <div /> <body /> <html /> - drugs_and_code
    • @ TolikBuguera, updated the answer - YuriySPb