Search code examples
javastringjavabeans

Java read part of string


I have this text :

   <message id="dsds" to="[email protected]" type="video" from="test@test"><body>TESTTESTTEST</body><active xmlns="http://jabber.org"/></message>

And I want to get the part of <body></body> in this string.

In java, I m searching and found split, but it cant solve my problem. How can I get the text between <body></body> in java?


Solution

  • Use regx package:

        String htmlString = "<message id=\"dsds\" to=\"[email protected]\" type=\"video\" from=\"test@test\"><body>TESTTESTTEST</body><active xmlns=\"http://jabber.org\"/></message>";
        String bodyText="";
        Pattern p = Pattern.compile("<body.*>(.*?)</body.*>");
        Matcher m = p.matcher(htmlString);
    
        if (m.find()) {
            bodyText = m.group(1);
        }
        System.out.println(bodyText);
    

    OUTPUT: TESTTESTTEST