Java xml delete element

I have a very simple xml file that I would like to create a simple function to remove an element 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 just want to call a method to remove a single name from a file. I am not very familiar with XML, but I was able to create a reader and writer, but now I am unable to create a method to remove an element 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);
            }
        }

    }
+5
source share
3 answers

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);
  }
+7
source

first select an item with good text.

To do this, use the xpath syntax: / book / person [name / text () = "Person 3"]

node, .

( ):

InputSource source = new InputSource(new FileInputStream(##your file##));

XPathFactory builder = XPathFactory.newInstance();
XPath xpath = builder.newXPath();

String label = "Person 3";
XPathExpression exp = xpath.compile("/book/person[name/text() = \"" + label +"\"]");

Node node = (Node) exp.evaluate(source, XPathConstants.NODE);
node.getParentNode().removeChild(node);
+1

Basically you need to parse the document and get the item and delete it.
You can do this with javax.xml.parsers and javax.xml.transform packages.

File file = new File(xmlFile);

xmlfile stores the xml file name. Read the file as a document.

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(xmlFile);
  TransformerFactory tFactory = TransformerFactory.newInstance();
  Transformer tFormer = tFactory.newTransformer();<br>

Then get the item and delete it as shown below.

Element element = (Element)doc.getElementsByTagName(remElement).item(0);
//  Remove the node
  element.getParentNode().removeChild(element);
0
source

All Articles