文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>使用jdom读取xml文件例子

使用jdom读取xml文件例子

时间:2007-08-20  来源:linuxchao

程序如下: import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import java.util.*;
public class TestDOM4j {
  public TestDOM4j() {
   
  }
  public static void main(String[] args) {
    TestDOM4j testDOM4j1 = new TestDOM4j();
    File xmlfile=new File("c:\\testc.xml");
   
    DOMBuilder builder=new DOMBuilder();
    try{
      Document doc = builder.build(xmlfile);
      Element rootelement=doc.getRootElement();
      List childlist =rootelement.getChildren();
      for (int i=0;i<childlist.size();i++){
        Element child=(Element)childlist.get(i);
        System.out.println("Loop i:"+i);
        System.out.println(child.getChild("name").getText());
        System.out.println(child.getChild("college").getText());
        System.out.println(child.getChild("telephone").getText());
        System.out.println(child.getChild("notes").getText());
 
      }
      List childlist1=rootelement.getChildren("person");
      for (int j=0;j<childlist1.size();j++){
         
        Element child=(Element)childlist.get(j);
       System.out.println("Loop i:"+j);
       System.out.println(child.getChild("name").getText());
       System.out.println(child.getChild("college").getText());
       System.out.println(child.getChild("telephone").getText());
       System.out.println(child.getChild("notes").getText());
      }
      List childlist2=rootelement.getChildren("name");
      System.out.println("name list size :="+childlist2.size());
      for (int k=0;k<childlist2.size();k++){
        Element childa=(Element)childlist2.get(k);
        System.out.println(childa.getText());
      }
      
    }catch(JDOMException e){
     
    }
   
   
  }
}   xml file ru xia : <?xml version="1.0" encoding="gb2312"?>
<persons>
 <person>
  <name>abcdefrasd</name>
  <college>材料学院</college>
  <telephone>6258113</telephone>
  <notes>男,1979年生,本科,99年调入中国陶瓷大学</notes>
 </person>
 <person>
  <name>张三</name>
  <college>大学</college>
  <telephone>1234567</telephone>
  <notes>男,本科,99年调入中国陶瓷大学</notes>
 </person>
</persons>
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载