import java.io.StringReader; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.xml.sax.InputSource; public class Test {
@SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { String xml = "<servlet><description></description><display-name>TestServlet</display-name><servlet-name>TestServlet</servlet-name><servlet-class>test.TestServlet</servlet-class></servlet>"; InputSource in = new InputSource(new StringReader(xml)); in.setEncoding("UTF-8"); SAXReader reader = new SAXReader(); Document document = reader.read(in); Element root = document.getRootElement(); List<Element> elements = root.elements(); for(Iterator<Element> it = elements.iterator();it.hasNext();){ Element element = it.next(); System.out.println(element.getName()+" : "+element.getTextTrim()); }