当前位置:   article > 正文

利用XML传输数据_若依 读取xml文件

若依 读取xml文件

一、客户端以XML格式向服务器端发送数据,并解析XML输出到控制台

要求如下:

客户端以XML格式向服务器端发送数据,详细要求如下:

1)客户端读取myemp.xml数据,将其发送到服务器端。

2)服务器接收到XML格式的数据后,进行解析,将解析到的数据输出到控制台。


代码如下:Emp类

  1. public class Emp {
  2. private int id;
  3. private String name;
  4. private int age;
  5. private String gender;
  6. private int salary;
  7. public Emp(int id, String name, int age, String gender, int salary) {
  8. super();
  9. this.id = id;
  10. this.name = name;
  11. this.age = age;
  12. this.gender = gender;
  13. this.salary = salary;
  14. }
  15. public int getId() {
  16. return id;
  17. }
  18. public void setId(int id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public int getAge() {
  28. return age;
  29. }
  30. public void setAge(int age) {
  31. this.age = age;
  32. }
  33. public String getGender() {
  34. return gender;
  35. }
  36. public void setGender(String gender) {
  37. this.gender = gender;
  38. }
  39. public int getSalary() {
  40. return salary;
  41. }
  42. public void setSalary(int salary) {
  43. this.salary = salary;
  44. }
  45. public String toString(){
  46. return id+","+name+","+age+","+gender+","+salary;
  47. }
  48. }
客户端:

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.net.Socket;
  5. import java.net.UnknownHostException;
  6. import org.dom4j.Document;
  7. import org.dom4j.io.SAXReader;
  8. import org.dom4j.io.XMLWriter;
  9. /**
  10. * 客户端
  11. */
  12. public class Client {
  13. private Socket socket;
  14. public Client() throws UnknownHostException, IOException{
  15. socket = new Socket("192.168.1.100",8088);
  16. }
  17. public void start(){
  18. try {
  19. //获取输出流
  20. OutputStream out = socket.getOutputStream();
  21. File file = new File("./src/day08/myemp.xml");
  22. //创建SAXReader,读取指定文件
  23. SAXReader reader = new SAXReader();
  24. Document doc = reader.read(file);
  25. //创建XML输出流
  26. XMLWriter writer = new XMLWriter();
  27. writer.setOutputStream(out);
  28. writer.write(doc);
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. } finally {
  32. if(socket != null){
  33. try {
  34. socket.close();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41. public static void main(String[] args) {
  42. try {
  43. Client c = new Client();
  44. c.start();
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. System.out.println("客户端初始化失败");
  48. }
  49. }
  50. }
服务端:

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.dom4j.Document;
  8. import org.dom4j.Element;
  9. import org.dom4j.io.SAXReader;
  10. /**
  11. * 服务端
  12. */
  13. public class Server {
  14. private ServerSocket server;
  15. public Server() throws IOException{
  16. server = new ServerSocket(8088);
  17. }
  18. public void start(){
  19. try {
  20. //监听客户端,建立连接
  21. System.out.println("等待连接..........");
  22. Socket socket = server.accept();
  23. System.out.println("服务端连接成功!");
  24. //获取输入流
  25. InputStream in = socket.getInputStream();
  26. //创建SAXReader,读取指定文件
  27. SAXReader reader = new SAXReader();
  28. Document doc = reader.read(in);
  29. //获取根节点
  30. Element root = doc.getRootElement();
  31. //获取根节点(list)下所有子节点emp
  32. List<Element> list = root.elements();
  33. //保存所有员工对象的集合
  34. List<Emp> emplist = new ArrayList<Emp>();
  35. for(Element e : list){
  36. int id = Integer.parseInt(e.attribute("id").getValue());
  37. String name = e.elementText("name");
  38. int age = Integer.parseInt(e.elementText("age"));
  39. String gender = e.elementText("gender");
  40. int salary = Integer.parseInt(e.elementText("salary"));
  41. emplist.add(new Emp(id,name,age,gender,salary));
  42. }
  43. System.out.println("解析完毕!");
  44. //输出到控制台
  45. for(Emp e : emplist){
  46. System.out.println(e);
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. } finally {
  51. if(server != null){
  52. try {
  53. server.close();
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }
  60. public static void main(String[] args) {
  61. try {
  62. Server s = new Server();
  63. s.start();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. System.out.println("初始化服务端失败!");
  67. }
  68. }
  69. }
测试结果会在服务端输出:

  1. 等待连接..........
  2. 服务端连接成功!
  3. 解析完毕!
  4. 1,jack,33,男,5000
  5. 2,boss,55,男,5500
  6. 3,marry,22,女,3500
  7. 4,kate,25,女,6000
  8. 5,tom,31,男,8500

二、客户端以XML格式向服务器端发送数据

要求如下:

客户端以XML格式向服务器端发送数据,详细要求如下:

1)客户端读取myemp.xml中的数据,将其发送到服务器端。

2)服务器接收到XML格式的数据后,将其输出到serverMyemp.xml文件中。

代码如下:

客户端:

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.net.Socket;
  5. import java.net.UnknownHostException;
  6. import org.dom4j.Document;
  7. import org.dom4j.io.SAXReader;
  8. import org.dom4j.io.XMLWriter;
  9. public class Client {
  10. private Socket socket;
  11. public Client() throws UnknownHostException, IOException{
  12. socket = new Socket("192.168.1.100",8088);
  13. }
  14. public void start(){
  15. try{
  16. OutputStream out = socket.getOutputStream();
  17. //创建SAXReader,读取指定文件
  18. SAXReader reader = new SAXReader();
  19. File file = new File("myemp.xml");
  20. Document doc = reader.read(file);
  21. //创建XML输出流
  22. XMLWriter writer = new XMLWriter();
  23. writer.setOutputStream(out);
  24. writer.write(doc);
  25. } catch(Exception e){
  26. e.printStackTrace();
  27. } finally {//只关socket
  28. if(socket != null){
  29. try {
  30. socket.close();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }
  37. public static void main(String[] args) {
  38. Client c;
  39. try {
  40. c = new Client();
  41. c.start();
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. System.out.println("客户端初始化失败!");
  45. }
  46. }
  47. }
服务端

代码如下:

  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import org.dom4j.Document;
  7. import org.dom4j.io.SAXReader;
  8. import org.dom4j.io.XMLWriter;
  9. public class Server {
  10. private ServerSocket server;
  11. public Server() throws IOException{
  12. server = new ServerSocket(8088);
  13. }
  14. public void start(){
  15. XMLWriter writer = null;
  16. try{
  17. System.out.println("等待连接......");
  18. Socket socket = server.accept();
  19. System.out.println("连接客户端成功!");
  20. InputStream in = socket.getInputStream();
  21. //读取用户发送过来的xml
  22. SAXReader reader = new SAXReader();
  23. Document doc = reader.read(in);
  24. //写出xml到day08包中
  25. FileOutputStream fos = new FileOutputStream("./src/day09/serverMyemp.xml");
  26. writer = new XMLWriter();
  27. writer.setOutputStream(fos);
  28. writer.write(doc);
  29. System.out.println("服务端接受文件成功!");
  30. } catch(Exception e){
  31. e.printStackTrace();
  32. } finally {//writer和server都要关
  33. if(writer != null){
  34. try {
  35. writer.close();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. if(server != null){
  41. try {
  42. server.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. }
  49. public static void main(String[] args) {
  50. try {
  51. Server s = new Server();
  52. s.start();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. System.out.println("服务端初始化失败!");
  56. }
  57. }
  58. }

服务端测试结果如下:

  1. 等待连接......
  2. 连接客户端成功!
  3. 服务端接受文件成功!





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/238753
推荐阅读
相关标签
  

闽ICP备14008679号