import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class CreateNewFile {
	/**
	 * @param args
	 */
	public static void main(String[] args){
	 Document document = DocumentHelper.createDocument();
	    Element root = document.addElement( "root" );

	    Element author2 = root.addElement( "author" )
	      .addAttribute( "name", "Toby" )
	      .addAttribute( "location", "Germany" )
	      .addText( "Tobias Rademacher" );
	    //author2.add(Namespace.NO_NAMESPACE);
	    
	    Element author1 = root.addElement( "author" )	      
	      .addAttribute( "name", "James" )
	      .addAttribute( "location", "UK" )
	      .addText( "James Strachan" );
	    //author1.add(Namespace.NO_NAMESPACE);
	    
	    Element booki = author1.addElement( "book" )
	      .addAttribute( "name", "James" )
	      .addAttribute( "location", "UK" )
	      .addText( "James bond" );
	    
	    
	    Namespace ns = new Namespace("","http://www.w3.org/2001/06/grammar");
	    
	    document.getRootElement().add(ns);
	    
  
	    Iterator iter =  document.getRootElement().selectNodes("author").iterator();
	    while(iter.hasNext())
	    {
	    	Element e = (Element) iter.next();
	    	e.remove(e.getNamespace());
	    	e.setQName(QName.get(e.getName(), ns.NO_NAMESPACE));

	    	
	    	//e.add(Namespace.NO_NAMESPACE);
	    	//e.setQName( QName.get(e.getName(), ns.NO_NAMESPACE,
	    		//	e.getQualifiedName() ) );

	    	//e.remove(e.getNamespace());
	    	//e.addAttribute("tt","bb"); 
	    }
	    
	    
	    OutputFormat outformat = OutputFormat.createPrettyPrint();
	    outformat.setEncoding("ISO-8859-1");
	    XMLWriter writer;
		try {
			//writer = new XMLWriter(System.out, outformat);
			writer = new XMLWriter(System.out,outformat);
	    writer.write(document);
	    writer.flush();
	    
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	    
	    
	}



}
