Search code examples
javaxmldomduplicatesdom4j

Remove duplicates in org.dom4j.Document object


Is it somehow possible to filter all duplicate elements on the same level in an org.dom4j.Document object in Java?

For example

<parent>
    <child><value>1</value></child>
    <child><value>1</value></child>
    <child><value>3</value></child>
</parent>

should lead to

<parent>
    <child><value>1</value></child>
    <child><value>3</value></child>
</parent>

Is there already a built in functionality for this? Or maybe a library that one could use?


Solution

  • No functionality for this.

    The org.w3c.dom.Node interface provides the removeChild method Code example

    xmlDoc=loadXMLDoc("books.xml");
    
    y=xmlDoc.getElementsByTagName("book")[0];
    
    xmlDoc.documentElement.removeChild(y)
    

    Or use XSLT

    - Documentation : http://docs.oracle.com/cd/B19306_01/appdev.102/b14252/adx_j_xslt.htm
    - Example : http://stackoverflow.com/a/10914512/4017037