import java.util.*;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
public class JdomJava{
public static void main(String[] args){
//当前时间,单位是毫秒
long lasting = System.currentTimeMillis();
try{
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(new File("a.xml"));
Element element = doc.getRootElement();
List allChildren = element.getChildren();
for(int i = 0;i<allChildren.size();i++){
System.out.println("卡号:"+((Element)allChildren.get(i)).getChild("code").getText());
System.out.println("密码:"+((Element)allChildren.get(i)).getChild("pass").getText());
System.out.println("姓名:"+((Element)allChildren.get(i)).getChildText("name"));
System.out.println("余额:"+((Element)allChildren.get(i)).getChildText("money"));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
下面是我的(a.xml)文档是我copy过来的:
<?xml version="1.0" encoding="gbk"?>
<Accounts>
<Account type="by0003">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>1000000.00</money>
</Account>
<Account type="hz0001">
<code>100002</code>
<pass>123</pass>
<name>张三</name>
<money>1000.00</money>
</Account>
</Accounts>
这里面还要导入jdom.jar包,大家可以从这里下载:
http://download.youkuaiyun.com/detail/changxiaoting/5663863