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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 for giving the bigger picture. I think a solution better than regexes will roll out. – Thomas Jul 16 '10 at 17:39
  • is this something that is being done from the web, or a thin client or swing, or other? – Matthew J Morrison Jul 16 '10 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... – Masiar Ighani Jul 16 '10 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 – Matthew J Morrison Jul 16 '10 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. – Masiar Ighani Jul 16 '10 at 18:25

1 Answers1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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... – Masiar Ighani Jul 16 '10 at 17:46