Search code examples
javahtmlunit

How to get text wrapped with <br></br>


I wanted to get the text inside break html but this it's not wrap with paragraph just a text inside break. Division is my only identifier for this which is "Msg". How can I get one of each element?

<div name="Msg"style="display:block">
     <b>
       HI! 
     </b>
         2017-11-30-15.28.09.133

     <br/>
     <b>
         123
     </b>
         abc
    <br/>
       Your number is 5.
    <br/>
</div>

Draft:

HtmlElement msg = (HtmlElement) page.getFirstByXPath("//div[@name='Msg']/text()");
System.err.println(msg.getTextContent());

Solution

  • Understood correctly you might want the code output as

    Your number is 5.
    <b>123</b>
    <b>123</b>
    <b>HI!</b>2017-11-30-15.28.09.133

    Maybe you might want to try with StringTokenizer (the codes below will hit java.util.NoSuchElementException as the nextToken() call is forced, but hopefully it will become a general idea)

    import java.util.StringTokenizer;
    ..
    String sampleStr= "Hello <br/> there <br/>";
    StringTokenizer token = new StringTokenizer(sampleStr);
    System.out.println(token.nextToken());//You can use .trim()
    System.out.println(token.nextToken());
    

    Would this help?