Remove an attribute in Java

Description

The following code shows how to remove an attribute.

Example


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//from   ww  w.j  a v  a2  s.com
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
  public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.newDocument();
    Element root = document.createElement("order");
    root.setAttribute("src", "java2s.com");
    document.appendChild(root);
    
    root.removeAttribute("src");
    
    System.out.println(root +" "+ root.getAttribute("src"));
    System.out.println(document);
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    XML »




DOM
SAX