Java regular expression to remove empty xml nodes and childrens completely

I am struggling to find the best solution. Below is my XML :

                <Dbtr>
                    <Nm>John doe</Nm>
                    <Id>
                        <OrgId>
                            <Othr>
                                <Id/>
                             </Othr>
                        </OrgId>
                    </Id>
                </Dbtr>

This is should replaced like this below :

                <Dbtr>
                    <Nm>John doe</Nm>
                </Dbtr>

So all the empty nodes and children without any values should be left out.
I am using following expression and it don’t work as per my wishes

docStr = docStr.replaceAll("<(w+)></1>|<w+/>", ""); 

Any help would be really appreciated.


Source: regex

Leave a Reply