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

  • Home
  • SEARCH
  • 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 1080751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:04:44+00:00 2026-05-16T22:04:44+00:00

In the below sample XML, how to remove Entire B Node if E=13 using

  • 0

In the below sample XML, how to remove Entire B Node if E=13 using java parser.

<xml>
   <A>
     <B>
       <C>
         <E>11</E>
         <F>12</F>
       </C>
    </B>
    <B>
       <C>
         <E>13</E>
         <F>14</F>
      </C>
    </B>
  </A>

Please advise.

  • 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-05-16T22:04:45+00:00Added an answer on May 16, 2026 at 10:04 pm

    Alternative DOM Approach

    Alternatively, instead of doing a brute force traversal of the XML document you could use the XPath capabilities in the JDK to find the “B” element with value “13” and then remove it from its parent:

    import java.io.File;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.xpath.*;
    import org.w3c.dom.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            Document document = dbf.newDocumentBuilder().parse(new File("input.xml"));
    
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();
            XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]");
    
            Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
            b13Node.getParentNode().removeChild(b13Node);
    
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer();
            t.transform(new DOMSource(document), new StreamResult(System.out));
        }
    
    }
    

    The advantage of using an XPath it’s easier to maintain, if the structure changes it’s just a one line change to your code. Also if the depth of your document grows the XPath based solution stays the same number of lines.

    Non-DOM Approach

    If you don’t want to materialize your XML as a DOM. You could use a Transformer and a stylesheet to remove a node:

    • http://download.oracle.com/javase/6/docs/api/javax/xml/transform/Transformer.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you please suggest the best approach for my requirement(below) in Java. I have
Below is sample code to create a radio button element with Yes/No options in
In the below code sample, what does {0:X2} mean? This is from the reflection
I've been learning C#, and I'm trying to understand lambdas. In this sample below,
The code below shows a sample that I've used recently to explain the different
I am getting a web service response as below. It has additional XML tag
I am using VSTS2008 + C# + .Net 3.0. I am using below code
The simple demo below captures what I am trying to do. In the real
Below I have a very simple example of what I'm trying to do. I
Below is the code of a simple html with a table layout. In FF

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.