Monday, 1 August 2011

Accessing Attributes Using JDOM

   


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.adapters.XercesDOMAdapter;
import org.jdom.input.DOMBuilder;

public class JDOMCreateExample {

  private static DOMBuilder builder = null;

  public static void main(String args[]) throws Exception, FileNotFoundException {

    XercesDOMAdapter xercAdapter = new XercesDOMAdapter();
    org.w3c.dom.Document w3Dom = xercAdapter.getDocument(new FileInputStream("games.xml")false);

    builder = new DOMBuilder("org.jdom.adapters.XercesDOMAdapter");

    Document doc = builder.build(w3Dom);

    List childs = doc.getRootElement().getChildren("game");

    Iterator itr = childs.iterator();
    while (itr.hasNext()) {
      Element child = (Elementitr.next();
      System.out.println(child.getName() " = " + child.getText());
      System.out.println(child.getAttributeValue("genre"));

    }

  }

}

   
    
  

No comments:

Post a Comment