读取XML文件里的数据,然后筛选数据写入文件中

package com.isoft;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestXMLMain
{
    /**
     * 解析XML
     * <功能详细描述>
     * @param stuList
     * @return [参数说明]
     * 
     * @return List<StudentVO> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static List<StudentVO> resXML()
    {
        List<StudentVO> stuList = new ArrayList<StudentVO>();//学生集合
        try
        {
            DocumentBuilderFactory domFac = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = domFac.newDocumentBuilder();
            InputStream is = new FileInputStream("E:\\Student.xml");
            Document dom = builder.parse(is);
            NodeList nodeList = dom.getElementsByTagName("stutent"); //得到所有student信息
            for (int i = 0; i < nodeList.getLength(); i++)
            {
                StudentVO stu = new StudentVO();//存进去的时候,要重新New一下
                Element node = (Element)nodeList.item(i);
                stu.setStuName(node.getAttribute("name"));//把名字放到对象中
                NodeList childList = node.getChildNodes();
                for (int j = 0; j < childList.getLength(); j++)
                {
                    Node child = childList.item(j);
                    if ("city".equals(child.getNodeName()))//把城市放到对象中
                    {
                        stu.setStuCity((child.getTextContent()));
                    }
                    if ("score".equals(child.getNodeName()))//把得分放到对象中
                    {
                        stu.setStuScore(Double.parseDouble(child.getTextContent()
                                .toString()));
                    }
                }
                stuList.add(stu);
            }
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return stuList;
    }
    
    /**
     * 排序
     * <功能详细描述>
     * @param list
     * @return [参数说明]
     * 
     * @return List<StudentVO> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static List<StudentVO> array(List<StudentVO> list)
    {
        StudentVO stuTemp = null;
        //冒泡排序
        for (int i = 0; i < list.size(); i++)
        {
            for (int j = 0; j < list.size() - i - 1; j++)
            {
                stuTemp = new StudentVO();
                if (list.get(j).getStuScore() < list.get(j + 1).getStuScore())
                {
                    stuTemp = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, stuTemp);
                }
            }
        }
        //取出城市为南京的学生信息
        List<StudentVO> newList = new ArrayList<StudentVO>();
        for (int i = 0; i < list.size(); i++)
        {
            if ("南京".equals(list.get(i).getStuCity())
                    && list.get(i).getStuScore() > 275.0)//南京学生,分数大于275.0
            {
                newList.add(list.get(i));
            }
        }
        return newList;
    }
    
    /**
     * 写入文件
     * <功能详细描述>
     * @param stu [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static void write(List<StudentVO> stu)
    {
        File file = new File("E:\\result.txt");
        try
        {
            file.createNewFile();
            FileWriter resultFile = new FileWriter(file);
            PrintWriter myFile = new PrintWriter(resultFile);
            
            for (int i = 0; i < stu.size(); i++)
            {
                //每行每行写入
                myFile.println(stu.get(i).getStuName() + "  "
                        + stu.get(i).getStuCity() + "  "
                        + stu.get(i).getStuScore());
                if (i > 3)
                {
                    break;
                }
            }
            myFile.close();
            resultFile.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * 程序入口
     * <功能详细描述>
     * @param args [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args)
    {
        List<StudentVO> sList = resXML();//解析XML
        List<StudentVO> stuList = array(sList);//排序
        write(stuList);//写入文件
    }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fee_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值