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 7878415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:39:10+00:00 2026-06-03T03:39:10+00:00

I am using following code to remove some specific nodes from xml file..It show

  • 0

I am using following code to remove some specific nodes from xml file..It show correct output on console
but somehow xml file is not getting updated..the contents remains as it is..
please help..
Thanks…

package com.sumit.xml.client;

import java.io.*;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;

public class ReadCDATA {
  static public void main(String[] arg) {
  try{
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter a XML file name: ");
  String xmlFile = bf.readLine();
  File file = new File(xmlFile);
  System.out.print("Enter an element which have to delete: ");
  String remElement = bf.readLine();
  if (file.exists()){
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(xmlFile);
  TransformerFactory tFactory = TransformerFactory.newInstance();
  Transformer tFormer = tFactory.newTransformer();
  Element element = (Element)doc.getElementsByTagName(remElement).item(0);
//  Remove the node
  element.getParentNode().removeChild(element);
//  Normalize the DOM tree to combine all adjacent nodes
  doc.normalize();
  Source source = new DOMSource(doc);
  Result dest = new StreamResult(System.out);
  tFormer.transform(source, dest);
  System.out.println();

  }
  else{
  System.out.println("File not found!");
  }
  }
  catch (Exception e){
  System.err.println(e);
  System.exit(0);
  }
  }
} 
  • 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-03T03:39:11+00:00Added an answer on June 3, 2026 at 3:39 am

    DOM is changed in process memory only. There’s nothing in code you shown which would write data to the underlying XML file transparently.

    You need to add something like

    createOutputFile("output.xml", transformToString(document));
    
    private static String transformToString(Document document) {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(new DOMSource(document), new StreamResult(buffer));
            return buffer.toString();
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }
    
    
    private static void createOutputFile(String filePath, String content) {
        FileWriter writer = null;
        try {
            try {
                writer = new FileWriter(filePath);
                writer.write(content);
            } finally {
                if (writer != null) {
                    writer.flush();
                    writer.close(); 
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
I am using the following code to remove a attribute from session List<User> l=(List<User>)
The following code is from a PIC microcontroller header file, but I assume it's
I have extracted the following code from selenium IDE.(c# remote control) using System; using
I am using following code to design my home page. The output (as shown
All, I'm removing some select options with jQuery using the following code: $('#other_liked_vendors option[value='
I'm having some issues using the following code on user input: htmlentities($string, ENT_COMPAT, 'UTF-8');
All, I'm using the following code to generate some li items in an ol.
I read a file's content into a byte array using the following code: public
I am using the following code but don't know why thumbnails are not created.

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.