Q:

Remove empty p tag from String variable using Java?

I'm trying to remove p tags which don't contain any text. And if any p tag contain text, but not contain any parent tag, So I am try to create a Parent tag DIV. I am trying to 
convert from org.jsoup.nodes.Document to org.w3c.dom.Document. 
Is it possible or any short cut solution?
Java code:
private void modifyMediaVariantContent(String html) {

    org.jsoup.nodes.Document doc = Jsoup.parse(html);

    for (org.jsoup.nodes.Element element : doc.select("*")) {
        if (!element.hasText() && element.isBlock()) {
            element.remove();
        }
    }
}

HTML String value: 
Before:
<p id="Id44">see the image and see the color... ?</p>
<p id="Id40"></p>
<div id="Id87" style="display:inline-block">
<video id="Id30" src="http://Id3.qa.cete.us/117973/video.mp4"></video>
</div>
<p id="Id28"></p>
<p id="Id-1"></p>
<div id ="Id21">
<img id="img_44186" src="/129884/apple.jpg" />
</div>
<p id="Id-320046-3-21"></p>

After:: Result:
<div>
<div id = "passageContent">
<p id="Id44">see the image and see the color... ?</p>
<div>
<div id="Id87" style="display:inline-block">
<video id="Id30" src="http://Id3.qa.cete.us/117973/video.mp4"></video>
</div>
<div id ="Id21">
<img id="img_44186" src="/129884/apple.jpg" />
</div>
</div>

or Result:
<div>
<p id="Id44">see the image and see the color... ?</p>
<div id="Id87" style="display:inline-block">
<video id="Id30" src="http://Id3.qa.cete.us/117973/video.mp4"></video>
</div>
<div id ="Id21">
<img id="img_44186" src="/129884/apple.jpg" />
</div>
</div>

A:

Have a look at the following snippet:
public class Test {

    public static void main(String[] args) {
        try {
            String html = "<p id=\"Id44\">see the image and see the color... ?</p>\r\n" + "<p id=\"Id40\"></p>\r\n"
                    + "<div id=\"Id87\" style=\"display:inline-block\">\r\n"
                    + "<video id=\"Id30\" src=\"http://Id3.qa.cete.us/117973/video.mp4\"></video>\r\n" + "</div>\r\n"
                    + "<p id=\"Id28\"></p>\r\n" + "<p id=\"Id-1\"></p>\r\n" + "<div id =\"Id21\">\r\n"
                    + "<img id=\"img_44186\" src=\"/129884/apple.jpg\" />\r\n" + "</div>\r\n" + "<p id=\"Id-320046-3-21\"></p>";
            new Test().modifyMediaVariantContent(html);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void modifyMediaVariantContent(String html) {
        org.jsoup.nodes.Document doc = Jsoup.parse(html);
        for (org.jsoup.nodes.Element element : doc.getElementsByTag("p")) {
            if (!element.hasText() && element.isBlock()) {
                element.remove();
            }
            if (element.hasText() && element.parent() == doc.body()) {
                Element replacment = new Element(Tag.valueOf("div"), "");
                replacment.appendChild(element.clone());
                element.replaceWith(replacment);
            }
        }

        System.out.println(doc.body().html());
    }
}

This outputs the following:
<div>
 <p id="Id44">see the image and see the color... ?</p>
</div>  
<div id="Id87" style="display:inline-block"> 
 <video id="Id30" src="http://Id3.qa.cete.us/117973/video.mp4"></video> 
</div>   
<div id="Id21"> 
 <img id="img_44186" src="/129884/apple.jpg"> 
</div>

To convert the Jsoup document to a org.w3c.dom.Document use org.jsoup.helper.W3CDom:
W3CDom w3cDom = new W3CDom();
org.w3c.dom.Document w3cDoc = w3cDom.fromJsoup(doc);

