任务目标
服务器:1.服务器将List对象存入XML文件中,XML文件名为emp5.xml
2.服务器读取XML文件,将文件发送给客户端
客户端:1.客户端接受服务器发送的XML文件,将文件存入磁盘
2.读取存入的XML文件,解析,然后转为List对象,在控制台打印输出
Emp.java
1 package XMLServiceAndClient; 2 3 public class Emp { 4 private int id; 5 private String name; 6 private int age; 7 private double salary; 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 public double getSalary() { 27 return salary; 28 } 29 public void setSalary(double salary) { 30 this.salary = salary; 31 } 32 @Override 33 public String toString() { 34 return "Emp [id=" + id + ", name=" + name + ", age=" + age 35 + ", salary=" + salary + "]"; 36 } 37 public Emp() { 38 } 39 public Emp(int id, String name, int age, double salary) { 40 super(); 41 this.id = id; 42 this.name = name; 43 this.age = age; 44 this.salary = salary; 45 } 46 47 48 }
服务器:
1 package XMLServiceAndClient; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 import java.net.ServerSocket; 9 import java.net.Socket; 10 import java.util.ArrayList; 11 import java.util.List; 12 13 import org.dom4j.Document; 14 import org.dom4j.DocumentHelper; 15 import org.dom4j.Element; 16 import org.dom4j.io.XMLWriter; 17 18 /** 19 *步骤: 1.服务器将List对象存入XML文件中,XML文件名为emp5.xml 20 * 2.服务器读取XML文件,将文件发送给客户端 21 */ 22 public class Service { 23 private ServerSocket ss; 24 private int port = 8080; 25 private List<Emp> list; 26 /**利用构造函数初始化ServerSocket*/ 27 public Service(){ 28 try { 29 ss = new ServerSocket(port); 30 list = new ArrayList<Emp>(); 31 list.add(new Emp(1,"张三",20,2000)); 32 list.add(new Emp(2,"李四",30,4000)); 33 list.add(new Emp(3,"王五",40,6000)); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 /**服务器启动*/ 39 public void start(){ 40 try { 41 Socket s = ss.accept();//等待客户端进入 42 OutputStream os = s.getOutputStream();//获取输出流 43 Document doc = DocumentHelper.createDocument();//获取document对象 44 Element root = doc.addElement("list"); 45 for (Emp emp : list) { 46 Element e = root.addElement("emp"); 47 e.addAttribute("id", ""+emp.getId()); 48 e.addElement("name").addText(emp.getName()); 49 e.addElement("age").addText(""+emp.getAge()); 50 e.addElement("salary").addText(""+emp.getSalary()); 51 } 52 FileOutputStream fos = new FileOutputStream(new File("emp5.xml")); 53 XMLWriter writer = new XMLWriter(); 54 writer.setOutputStream(fos); 55 writer.write(doc);//将document输出到XML文件 56 writer.close(); 57 fos.close(); 58 FileInputStream fis = new FileInputStream(new File("emp5.xml")); 59 byte[] buf = new byte[1024];//设置缓冲区大小 60 int size = -1; 61 while((size=fis.read(buf))!=-1){ 62 os.write(buf, 0, size); 63 } 64 fis.close(); 65 s.close(); 66 } catch (IOException e) { 67 e.printStackTrace(); 68 } 69 } 70 public static void main(String[] args) { 71 Service s = new Service(); 72 s.start(); 73 } 74 }
客户端:
1 package XMLServiceAndClient; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import java.net.Socket; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.dom4j.Document; 11 import org.dom4j.Element; 12 import org.dom4j.io.SAXReader; 13 14 15 /** 16 *步骤: 1.客户端接受服务器发送的XML文件,将文件存入磁盘 17 * 2.读取存入的XML文件,解析,然后转为List对象,在控制台打印输出 18 */ 19 public class Client { 20 private Socket s; 21 private int port = 8080; 22 private List<Emp> list; 23 24 public void start(){ 25 try { 26 s = new Socket("localhost", port); 27 InputStream is = s.getInputStream(); 28 FileOutputStream fos = new FileOutputStream(new File("emp6.xml")); 29 byte[] buf = new byte[1024];//设置缓冲区大小 30 int size = -1; 31 while((size=is.read(buf))!=-1){ 32 fos.write(buf, 0, size); 33 } 34 fos.close(); 35 s.close(); 36 list = new ArrayList<Emp>(); 37 SAXReader reader = new SAXReader(); 38 Document doc = reader.read(new File("emp6.xml")); 39 Element root = doc.getRootElement(); 40 List<Element> elements = root.elements(); 41 for (Element element : elements) { 42 Emp emp = new Emp(); 43 emp.setId(Integer.parseInt(element.attribute("id").getValue())); 44 emp.setName(element.element("name").getText()); 45 emp.setAge(Integer.parseInt(element.element("age").getText())); 46 emp.setSalary(Double.parseDouble(element.element("salary").getText())); 47 list.add(emp); 48 } 49 System.out.println(list); 50 } catch (Exception e) { 51 e.printStackTrace(); 52 } 53 } 54 55 public static void main(String[] args) { 56 Client c = new Client(); 57 c.start(); 58 59 } 60 }
时间: 2024-11-13 09:09:28