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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:26:53+00:00 2026-05-24T12:26:53+00:00

I have a very simple xml file that I would like to create a

  • 0

I have a very simple xml file that I would like to create a simple function to remove an item from it. Here is my xml file:

    <?xml version="1.0"?>
<book>
  <person>
    <name>Person 1</name>
  </person>
  <person>
    <name>Person 2</name>
  </person>
  <person>
    <name>Person 3</name>
  </person>
  <person>
    <name>Person 4</name>
  </person>
</book>

I simply want to call a method to delete one name from the file. I’m not very familiar with XML but did manage to create a reader and writer but now I’m having trouble creating a method to delete an item from my file.

When I say delete an item I mean:

deleteItem("Person 3");

And then the XML file will change to:

<?xml version="1.0"?>
    <book>
      <person>
        <name>Person 1</name>
      </person>
      <person>
        <name>Person 2</name>
      </person>
      <person>
        <name>Person 4</name>
      </person>
    </book>

What did I do wrong:

public static void removeName(String personName) throws ParserConfigurationException, IOException, SAXException{
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (new File("test.xml"));

        NodeList nodes = doc.getElementsByTagName("person");
        for (int i = 0; i < nodes.getLength(); i++) {       
            Element person = (Element)nodes.item(i);
            Element name = (Element)person.getElementsByTagName("name").item(0);
            String pName = name.getTextContent();
            if(pName.equals(personName)){
                person.getParentNode().removeChild(person);
            }
        }

    }
  • 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-24T12:26:54+00:00Added an answer on May 24, 2026 at 12:26 pm

    Using DOM:

      public static void main(String[] args) throws Exception {
        final String xml =    "<book> " +
            "<person> " +
              "<name>Person 1</name> " +
            "</person> " +
            "<person> " +
              "<name>Person 2</name> " +
            "</person> " +
            "<person> " +
              "<name>Person 3</name> " +
            "</person> " +
            "<person> " +
              "<name>Person 4</name> " +
            "</person> " +
          "</book>";
    
          DocumentBuilderFactory dbf =
              DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          InputSource is = new InputSource();
          is.setCharacterStream(new StringReader(xml));
    
          Document doc = db.parse(is);
          deletePerson(doc, "Person 3");
          printXML(doc);
      }
    
      public static void deletePerson(Document doc, String personName) {
        // <person>
        NodeList nodes = doc.getElementsByTagName("person");
    
        for (int i = 0; i < nodes.getLength(); i++) {
          Element person = (Element)nodes.item(i);
          // <name>
          Element name = (Element)person.getElementsByTagName("name").item(0);
          String pName = name.getTextContent();
          if (pName.equals(personName)) {
             person.getParentNode().removeChild(person);
          }
        }
      }
    
      public static void printXML(Document doc) 
      throws TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    
        String xmlString = result.getWriter().toString();
        System.out.println(xmlString);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a web.config file that looks like this: <?xml version=1.0?> <configuration> <system.web> <compilation
i have very simple problem. I need to create model, that represent element of
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have a very simple Java RMI Server that looks like the following: import
I have a very complex task - create a software that imports XMl files
I have a simple question about sending a file (XML file) from my webapp
I have a very simple bit of script that changes the status of an
I have a very simple ASP.Net page that acts as a front end for
I would like to see some examples of simple flat file databases and how
I'm trying to get data from a webpage that serves a XML file periodically

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.