3

I need a regex for following html :

<div xmlns="http://www.w3.org/1999/xhtml">    <p/>
  <p/><p/>    <p/>
</div>

This comes form a richtext field and obviously this is no meaningful content or means: empty. I can not say in java: if (richTextConent == null || richTextContent.length == 0) because the richtext field contains something. Semantically the above content is empty so i thought of using a regex. I need to match this snippet with java.util.regex

If there is something meaningful in the snippet like:

<div xmlns="http://www.w3.org/1999/xhtml"> text<p/>
  <p/><p/>text    <p/>
</div>

than the regex should not match.

5
  • +1 for giving the bigger picture. I think a solution better than regexes will roll out. Commented Jul 16, 2010 at 17:39
  • is this something that is being done from the web, or a thin client or swing, or other? Commented Jul 16, 2010 at 17:42
  • Well, there is a richtexfield in a cms system which delivers its content as XML Markup. I need to know if the content is empty to surpress its rendering in a jsp template. As you can see from the example the content is not empty as this third party cms delivers empty tags and a div. So i thought regex would be fine to find out... Commented Jul 16, 2010 at 17:44
  • Does that mean you don't have control over the interface of the CMS? If you did, I would maybe suggest using jQuery to get child nodes with text, otherwise I would go with @BalusC's answer Commented Jul 16, 2010 at 17:47
  • i have control over the interface. The problem ist that there seems to be bug. If i enter something in the editor and delete it again, the api of the cms still delivers this "empty" xml markup:-( Thank you for your ideas. Commented Jul 16, 2010 at 18:25

1 Answer 1

3

Use a HTML parser like Jsoup.

String html1 = "<div xmlns=\"http://www.w3.org/1999/xhtml\">    <p/>  <p/><p/>    <p/></div>";
String html2 = "<div xmlns=\"http://www.w3.org/1999/xhtml\"> text<p/>        <p/><p/>text    <p/>        </div>";

System.out.println(Jsoup.parse(html1).text().isEmpty()); // true
System.out.println(Jsoup.parse(html2).text().isEmpty()); // false

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, i love Stackoverflow. It took only 4 minutes to get a qualified answer. I will give jsoup a try today. Thank you so far...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.