xml解析(dom java)

数据模型:

Student.java

 

package com.xie.xmlparse.dom4j.modal;

public class Student {
       private Long classId;
       private Long stuId;
       private String stuName;
       private String stuSex;
       private int stuAge;
 public Long getClassId() {
  return classId;
 }
 public void setClassId(Long classId) {
  this.classId = classId;
 }
 public Long getStuId() {
  return stuId;
 }
 public void setStuId(Long stuId) {
  this.stuId = stuId;
 }
 public String getStuName() {
  return stuName;
 }
 public void setStuName(String stuName) {
  this.stuName = stuName;
 }
 public String getStuSex() {
  return stuSex;
 }
 public void setStuSex(String stuSex) {
  this.stuSex = stuSex;
 }
 public int getStuAge() {
  return stuAge;
 }
 public void setStuAge(int stuAge) {
  this.stuAge = stuAge;
 }
      
      
}

需要解析的xml

//student.xml

<?xml version="1.0" encoding="UTF-8"?>
<school>
     <class name="030713">
        <student>
           <stuid>03071300</stuid>
           <stuname>小明</stuname>
           <stusex>男</stusex>
           <stuage>10</stuage>
        </student>
        <student>
           <stuid>03071301</stuid>
           <stuname>小花</stuname>
           <stusex>女</stusex>
           <stuage>20</stuage>
        </student>
        <student>
           <stuid>03071302</stuid>
           <stuname>不知道</stuname>
           <stusex>男</stusex>
           <stuage>15</stuage>
        </student>       
     </class>
     <class name="030714">
        <student>
           <stuid>03071400</stuid>
           <stuname>小明</stuname>
           <stusex>男</stusex>
           <stuage>10</stuage>
        </student>
        <student>
           <stuid>03071401</stuid>
           <stuname>小花</stuname>
           <stusex>女</stusex>
           <stuage>20</stuage>
        </student>
        <student>
           <stuid>03071402</stuid>
           <stuname>不知道</stuname>
           <stusex>男</stusex>
           <stuage>15</stuage>
        </student>       
     </class>    
</school>

 

//解析xml的java代码

//XmlParse.java

 

package com.xie.xmlparse.dom;

import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.xie.xmlparse.dom4j.modal.Student;

//将xml中的数据读出
public class ParseXml {
      public ArrayList<Student> domParseXml(){
       long lasting = System.currentTimeMillis();
       ArrayList<Student> aslist=new ArrayList<Student>();
       try {
        File f=new File("D://project//XmlParse//xmlFiles//student.xml");
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document doc=db.parse(f);
        NodeList nodes=doc.getElementsByTagName("class");
     for (int i = 0; i < nodes.getLength(); i++) {
     if (nodes.item(i).getNodeType()==Node.ELEMENT_NODE) {
     
     Element ele=(Element)nodes.item(i);
     
     NodeList nodes1=ele.getElementsByTagName("student");
     
     for (int j = 0; j < nodes1.getLength(); j++) {
      
      if (nodes1.item(j).getNodeType()==Node.ELEMENT_NODE) {
       Student s=new  Student();
       Element ele1=(Element)nodes1.item(j);
       s.setClassId(Long.parseLong(ele.getAttribute("name")));
       s.setStuId(Long.parseLong(ele1.getElementsByTagName("stuid").item(0).getFirstChild().getNodeValue()));
       s.setStuName(ele1.getElementsByTagName("stuname").item(0).getFirstChild().getNodeValue());
       s.setStuSex(ele1.getElementsByTagName("stusex").item(0).getFirstChild().getNodeValue());
       s.setStuAge(Integer.parseInt(ele1.getElementsByTagName("stuage").item(0).getFirstChild().getNodeValue()));
       
       aslist.add(s);
       
       
/*       System.out.println(ele.getAttribute("name"));
       
       
       
       System.out.println(ele1.getElementsByTagName("stuid").item(0).getFirstChild().getNodeValue());
       
       System.out.println(ele1.getElementsByTagName("stuname").item(0).getFirstChild().getNodeValue());
       
       System.out.println(ele1.getElementsByTagName("stusex").item(0).getFirstChild().getNodeValue());
       
       System.out.println(ele1.getElementsByTagName("stuage").item(0).getFirstChild().getNodeValue());*/
      }
     }
    }
    }
      } catch (Exception e) {
   e.printStackTrace();
   }
         System.out.println("运行时间:"+(System.currentTimeMillis() - lasting)+" 毫秒");
      return aslist;
      }
      public static void main(String[] args){
       ArrayList< Student> a=new ParseXml().domParseXml();
       for (int i = 0; i < a.size(); i++) {
   Student s=a.get(i);
   System.out.println(s.getClassId());
   System.out.println(s.getStuId());
   System.out.println(s.getStuName());
   System.out.println(s.getStuSex());
   System.out.println(s.getStuAge());
   
  }
      }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值