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);//写入文件
}
}
读取XML文件里的数据,然后筛选数据写入文件中
最新推荐文章于 2022-09-20 13:19:25 发布