Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:24:37+00:00 2026-06-11T17:24:37+00:00

I want to delete the empty nodes from an XML element. This xml is

  • 0

I want to delete the empty nodes from an XML element.
This xml is generated from a vendor and i dont have control on xml generation. But since the XML has few empty nodes i need to delete those empty nodes recursively.

This xml is got from OMElement and i get an Element from this object using [XMLUtils][1]
Sample XML

<A>
  <B>
    <C>
      <C1>
        <C11>something</C11>
        <C12>something</C12>
      </C1>
    </C>
    <D>
      <D1>
        <D11>
          <D111 operation="create">
            <Node>something else</Node>
          </D11>
        </D11>
      </D1>
      <D2>
        <D21>

        </D21>
      </D2>
    </D>
  </B>
</A> 

Since D21 is an empty node i want to delete D21 and since now D2 is an empty node i want to delete D2 but since D has D1 i dont want to delete D.

Similarly it is possible that i can get

<A>
  <B>
    <C>

    </C>
  </B>
</A>

Now since C is empty i want to delete C and then B and then eventually node A.
I am trying to do this using removeChild() method in Node

But so far i am unable to remove them recursively.
Any suggestions to remove them recursively?

I am recursively trying to get node and node length. But node length is of no help

if(childNode.getChildNodes().getLength() == 0 ){
       childNode.getParentNode().removeChild(childNode);

               }

Regards
Dheeraj Joshi

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:24:38+00:00Added an answer on June 11, 2026 at 5:24 pm

    This works, just create a recursive function that “goes deep” first, then removes empty nodes on the way “back up the tree”, this will have the effect of removing both D21 and D2.

    public static void main(String[] args) throws Exception {
    
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        String input = "<A><B><C><C1><C11>something</C11><C12>something</C12></C1></C><D><D1><D11><D111 operation=\"create\"><Node>something else</Node></D111></D11></D1><D2><D21></D21></D2></D></B></A>";
    
        Document document = builder.parse(new InputSource(new StringReader(
                input)));
    
        removeNodes(document);
    
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(document), result);
        System.out.println(result.getWriter().toString());
    }
    
    public static void removeNodes(Node node) {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeNodes(list.item(i));
        }
        boolean emptyElement = node.getNodeType() == Node.ELEMENT_NODE
                && node.getChildNodes().getLength() == 0;
        boolean emptyText = node.getNodeType() == Node.TEXT_NODE
                && node.getNodeValue().trim().isEmpty();
        if (emptyElement || emptyText) {
            node.getParentNode().removeChild(node);
        }
    }
    

    Output

    <A>
    <B>
    <C>
    <C1>
    <C11>something</C11>
    <C12>something</C12>
    </C1>
    </C>
    <D>
    <D1>
    <D11>
    <D111 operation="create">
    <Node>something else</Node>
    </D111>
    </D11>
    </D1>
    </D>
    </B>
    </A>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete all the empty nodes in my XML document using SimpleXML
I want to delete an entry from my Database but it says it can't
I want to delete files from a specific directory recursively. So, I have used
H have multiple TextViews inside an LinearLayout . Now I want to delete this
I want to delete every second and third element from an array in Javascript.
i have this c# code and i want to delete a certain sub directory
Good morning, please, I have dependent GridView and I want delete row, but it
I have two tables: products(product_id,date) and sex(product_id,sex). I want to DELETE from the products
I want to delete all rows in a datatable. I use something like this:
I want to delete DataRows from a DataTable. The rows to be deleted are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.