tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 XML > XOM > Remove Attribute

Remove Attribute 

XOM (XML Object Model) is a tree based java API for processing XML by taking the best ideas from SAX and DOM. It is simple, fast and easy to use. This requires the library xom-1.2.7.jar to be in classpath. The following example shows removing an attribute from a specific element.

File Name  :  
/XOM001/config/randoms.xml 

File Name  :  
com/bethecoder/tutorials/xom/tests/RemoveAttribute.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.xom.tests;

import java.io.IOException;
import java.io.InputStream;

import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.ValidityException;

public class RemoveAttribute {

  /**
   @param args
   @throws IOException 
   @throws ParsingException 
   @throws ValidityException 
   */
  public static void main(String[] argsthrows ValidityException, ParsingException, IOException {
    Builder builder = new Builder();
    InputStream ins = RemoveAttribute.class.getClassLoader()
          .getResourceAsStream("randoms.xml");
    
    //Reads and parses the XML
    Document doc = builder.build(ins);

    //Get children
    Elements numbers = doc.getRootElement().getChildElements();
    Attribute evenAttribute = null;
    for (int i = ; i < numbers.size() ; i ++) {
      
      //Get the attribute by name
      evenAttribute = numbers.get(i).getAttribute("even");
      if (evenAttribute != null) {
        //Remove the attribute
        numbers.get(i).removeAttribute(evenAttribute);
      }
    }
    
    System.out.println(doc.toXML());
  }

}
   

It gives the following output,
File Name  :  OUTPUT



 
  


  
bl  br