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

Remove Child by Index 

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 a child element from parent by index.

File Name  :  
/XOM001/config/student_list.xml 

File Name  :  
com/bethecoder/tutorials/xom/tests/RemoveChildByIndex.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.xom.tests;

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

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

public class RemoveChildByIndex {

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

    //Get student list and
    //children of student
    // 0 - white space text node [depends on the XML formatting]
    // 1 - name node
    // 2 - white space text node
    // 3 - age node
    Elements students = doc.getRootElement().getChildElements();
    for (int i = ; i < students.size() ; i ++) {
      
      //Remove fourth child (Zero based index)
      System.out.println(students.get(i).removeChild(3));
    }
    
    System.out.println(doc.toXML());
  }

}
   

It gives the following output,
File Name  :  OUTPUT


                    Print

   



Books Recommendation

 
 
bl  br