Java HTML / XML How to - Use JDOM2








Question

We would like to know how to use JDOM2.

Answer

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
/*from   w  w  w .  j av  a 2  s  . c o  m*/
public class Main {

  public static void main(String[] args) throws Exception {
    Document document = new SAXBuilder().build(Test.class.getResourceAsStream("test.xml"));

    for(Element elt :document.getRootElement().getChildren()) {
      System.out.println("tag : "+elt.getName());
      System.out.println("value : " + elt.getText()+"\n");
    }
  }
}