public class TestElementLength
{
static String xmlContent;
public static void init()
{
xmlContent = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"
+ "<lib>"
+ "<book>"
+ "<name>Java</name>"
+"<author>tom</author>"
+"<author>jack</author>"
+ "</book>"
+ "<book>"
+ "<name>XML</name>"
+ "<pubdate>2002-10-07</pubdate>"
+ "</book>"
+ "</lib>";
} public static void outputXml()
{
try
{
org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
org.jdom.Document doc = sb.build(new java.io.
StringBufferInputStream(xmlContent));
org.jdom.Element root = doc.getRootElement();
java.util.List books = root.getChildren("book"); //得到根元素所有子元素的集合
for(int i=0;i<books.size();i++)
{
org.jdom.Element book =(org.jdom.Element) books.get(i);
System.out.println(book.getChild("name").getText());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
init();
outputXml();
}
}
基本实现都是一样的。