温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<data>
    
<student id="1">
        
<name>student0</name>
        
<age>10</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="2">
        
<name>student1</name>
        
<age>11</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="3">
        
<name>student2</name>
        
<age>12</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="4">
        
<name>student3</name>
        
<age>13</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="5">
        
<name>student4</name>
        
<age>14</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="6">
        
<name>student5</name>
        
<age>15</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="7">
        
<name>student6</name>
        
<age>16</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="8">
        
<name>student7</name>
        
<age>17</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="9">
        
<name>student8</name>
        
<age>18</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
    
<student id="10">
        
<name>student9</name>
        
<age>19</age>
        
<sex></sex>
        
<address>陕西西安</address>
    
</student>
</data>
handler
package com.sax;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.xmlpull.Student;

public class StudentHandler extends DefaultHandler {
    
    
private Student student=null;
    
private String tag;
    
private List<Student> list=null;
    
    
    
public List<Student> getList() {
        
return list;
    }

    
public void setList(List<Student> list) {
        
this.list = list;
    }
    
    
    
    @Override
    
public void characters(char[] ch, int start, int length)
            
throws SAXException {
        String data
=new String(ch,start,length);
        
if(null!=tag){
            
if("name".equalsIgnoreCase(tag)){
                student.setName(data);
            }
else if("age".equalsIgnoreCase(tag)){
                student.setAge(data);
            }
else if("sex".equalsIgnoreCase(tag)){
                student.setSex(data);
            }
else if("address".equalsIgnoreCase(tag)){
                student.setAddress(data);
            }
        }
    }

    @Override
    
public void endDocument() throws SAXException {
        
    }

    @Override
    
public void endElement(String uri, String localName, String name)
            
throws SAXException {
        
if("student".equalsIgnoreCase(name)){
            list.add(student);
            student
=null;
        }
        tag
=null;
    }

    @Override
    
public void startDocument() throws SAXException {
        list
=new ArrayList<Student>();
    }

    @Override
    
public void startElement(String uri, String localName, String name,
            Attributes attributes) 
throws SAXException {
        
if("student".equals(name)){
            student
=new Student();
            
if(attributes!=null){
                student.setId(Integer.parseInt(attributes.getValue(
0)));
            }
        }
        tag
=name;
    }
    
    
    
}
SaxXmlParse
package com.sax;

import java.io.File;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.xmlpull.Student;

/**
 * <pre>
 * sax解析xml
 * </pre>
 * 
@author scott
 *
 
*/
public class SaxXmlParse{
    
    
public List<Student> parseXML() throws Exception{
        SAXParserFactory parserFactory
=SAXParserFactory.newInstance();
        SAXParser parser 
= parserFactory.newSAXParser();
        
//XMLReader reader=parser.getXMLReader();
        StudentHandler studentHandler=new StudentHandler();
        
//reader.setContentHandler(studentHandler);
        
//reader.parse(new InputSource(new FileInputStream(new File("D:\\workspace\\demo\\src\\students.xml"))));
        parser.parse(new File("D:\\workspace\\demo\\src\\students.xml"), studentHandler);
        
return studentHandler.getList();
    }
    
    
    
public static void main(String[] args) throws Exception{
        List
<Student> list=new SaxXmlParse().parseXML();
        
for (Student stu : list) {
            System.out.println(stu.getId()
+"\t"+stu.getName()+"\t"+stu.getSex()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }
    
    
    
    
    
}
效果图
1 student0 女 10 陕西西安
2 student1 男 11 陕西西安
3 student2 女 12 陕西西安
4 student3 男 13 陕西西安
5 student4 女 14 陕西西安
6 student5 男 15 陕西西安
7 student6 女 16 陕西西安
8 student7 男 17 陕西西安
9 student8 女 18 陕西西安
10 student9 男 19 陕西西安



posted on 2011-08-24 11:36 雪山飞鹄 阅读(1415) 评论(0)  编辑  收藏 所属分类: javaseandroid

只有注册用户登录后才能发表评论。


网站导航: