题目:基于TCP/IP用DOM4j修改XML

(1)使用dom4j将信息存入xml中

(2)读取信息,并打印控制台

(3)添加一个city节点与子节点

(4)使用socket TCP协议编写服务端与客户端,

客户端输入城市ID,服务器响应相应城市信息

(5)使用socket TCP协议编写服务端与客户端,客户端要求用户输入city对象,服务端接收并使用dom4j保存至XML文件

xml文件:

XML 复制代码
1.
<?xml version="1.0" encoding="GBK"?>
<citys>
	<city id='010'>
		<cityname>北京</cityname>
		<cityarea>华北</cityarea>
		<population>2114.8万人</population>
	</city>
	<city id='021'>
		<cityname>上海</cityname>
		<cityarea>华东</cityarea>
		<population>2,500万人</population>
	</city>
	<city id='020'>
		<cityname>广州</cityname>
		<cityarea>华南</cityarea>
		<population>1292.68万人</population>
	</city>
	<city id='028'>
		<cityname>成都</cityname>
		<cityarea>华西</cityarea>
		<population>1417万人</population>
	</city>
</citys>

第一题:

java 复制代码
public class Save {
    public static void main(String[] args) {

        try {

            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("citys");//添加根节点

            Element element1 = root.addElement("city");
            element1.addAttribute("id","010");
            element1.addElement("cityname").setText("洛阳");
            element1.addElement("cityarea").setText("华中");
            element1.addElement("population").setText("1000");

            Element element2 = root.addElement("city");
            element2.addAttribute("id","011");
            element2.addElement("cityname").setText("北京");
            element2.addElement("cityarea").setText("华北");
            element2.addElement("population").setText("9000");


            //写入
            XMLWriter xmlWriter = new XMLWriter(new FileWriter("D:\\idea-workspace\\Book03\\ch010\\src\\QuestionText\\city.xml"));
            xmlWriter.write(document);
            xmlWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

第二题:

java 复制代码
package QuestionText.SecondQuestion;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class ReadElement {
    public static void main(String[] args) {
      //  这些是读入
        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read("D:\\idea-workspace\\Book03\\ch010\\src\\QuestionText\\city.xml");
            //获取根节点
            Element rootElement = document.getRootElement();
            //根节点下的所有子节点
            List<Element> firstElementList = rootElement.elements();
            for (Element element1 :firstElementList){

                String id = element1.attributeValue("id");
                String cityname = element1.elementText("cityname");
                String cityarea = element1.elementText("cityarea");
                String population = element1.elementText("population");

                System.out.println("id:"+id+"  cityname:"+cityname+"  cityarea:"+cityarea+"  population:"+population);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

第三题:

java 复制代码
public class AddOne {
    public static void main(String[] args) {

        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read("D:\\idea-workspace\\Book03\\ch010\\src\\QuestionText\\city.xml");

            Element rootElement = document.getRootElement();
            Element element = rootElement.addElement("city");
            element.addAttribute("id","022");
            element.addElement("cityname").setText("上海");
            element.addElement("cityarea").setText("华西");
            element.addElement("population").setText("980");


            //写入
            XMLWriter xmlWriter = new XMLWriter(new FileWriter("D:\\idea-workspace\\Book03\\ch010\\src\\QuestionText\\city.xml"));
            xmlWriter.write(document);
            xmlWriter.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

第四题:

服务端:
java 复制代码
public class Tcp {
    //使用socket TCP协议编写服务端与客户端,
    //客户端输入城市ID,服务器响应相应城市信息
    //服务端
    public static void main(String[] args) {

        System.out.println("服务端------------------------------------------------------------------");

        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();
            //获取输出流
            InputStream inputStream = socket.getInputStream();
            byte b[] = new byte[100];
            inputStream.read(b);
            String id = new String(b).trim();
            System.out.println(id);

            String cityString = selectById(id);
            //返回信息
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(cityString.getBytes());




        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String selectById(String id) {
        //接收到输入的以后在XML文件中寻找
//

        try {

            SAXReader saxReader = new SAXReader();
            Document document = null;
            document = saxReader.read("D:\\idea-workspace\\Book03\\ch010\\src\\QuestionText\\city.xml");
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();

            String msg = null;
            for (Element element : elements) {
                String cityid = element.attributeValue("id");
                if (id.equals(cityid)) {
                    msg += "id" + cityid;
                    msg += ",cityname" + element.elementText("cityname");
                    msg += ",cityarea" + element.elementText("cityarea");
                    msg += ",population" + element.elementText("population");
                }
            }
            return msg;
        }
        catch(Exception e){
               e.printStackTrace();
            }

        return  null;

        }
    }
客户端:
java 复制代码
public class TCPclient {
    public static void main(String[] args) {
        System.out.println("客户端------------------------------------------------------------------");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入城市id");
        String id = sc.next();
        try {
            Socket socket = new Socket("127.0.0.1",8888);
            OutputStream os = socket.getOutputStream();
            os.write(id.getBytes());
            //结束Output
            socket.shutdownOutput();


          //接收
            InputStream inputStream =  socket.getInputStream();

            byte b[] = new byte[1000];
            inputStream.read(b);
            String msg = new String(b).trim();
            System.out.println(msg);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

第五题:

客户端:
java 复制代码
public class TcpClient {
    public static void main(String[] args) {

        System.out.println("客户端------------------------------------------");
        try {
            Scanner sc = new Scanner(System.in);

            Socket socket = new Socket("127.0.0.1",8888);
            OutputStream os = socket.getOutputStream();
            City city = new City();
            System.out.println("请输入id:");
            city.setId(sc.next());
            System.out.println("请输入城市名:");
            city.setCityname(sc.next());
            System.out.println("请输入地区:");
            city.setCityarea(sc.next());

            System.out.println("请输入人口数量(万人):");
            city.setPopulation(sc.next()+"万人");


            //序列化输出流
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(city);



        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
服务端:
java 复制代码
public class TCP {
    public static void main(String[] args) {

        try {
            System.out.println("服务端");
            ServerSocket serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            City city = (City)ois.readObject();
            //插入XML

            String msg = editXml(city);

            System.out.println(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static String editXml(City city){

        try {

            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read("D:\\idea-workspace\\Book03\\ch010\\src\\QuestionText\\city.xml");
            Element rootElement = document.getRootElement();

            Element element = rootElement.addElement("city");
            element.addAttribute("id",city.getId());
            element.addElement("cityname").setText(city.getCityname());
            element.addElement("cityarea").setText(city.getCityarea());
            element.addElement("population").setText(city.getPopulation());

            //写入
            XMLWriter xmlWriter = new XMLWriter(new FileWriter("D:\\idea-workspace\\Book03\\ch010\\src\\QuestionText\\city.xml"));
            xmlWriter.write(document);
            xmlWriter.close();
            return "新增成功";



        } catch (Exception e) {
            e.printStackTrace();
           
        }

        return "新增失败";

    }
}
city对象:
java 复制代码
import java.io.Serializable;

public class City implements Serializable {
    private String id;
    private String cityname;
    private String cityarea;
    private String population;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCityname() {
        return cityname;
    }

    public void setCityname(String cityname) {
        this.cityname = cityname;
    }

    public String getCityarea() {
        return cityarea;
    }

    public void setCityarea(String cityarea) {
        this.cityarea = cityarea;
    }

    public String getPopulation() {
        return population;
    }

    public void setPopulation(String population) {
        this.population = population;
    }

    public City(String id, String cityname, String cityarea, String population) {
        this.id = id;
        this.cityname = cityname;
        this.cityarea = cityarea;
        this.population = population;
    }
    public City() {
    }
}
相关推荐
2202_754421543 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
蓝染-惣右介5 分钟前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
小林想被监督学习6 分钟前
idea怎么打开两个窗口,运行两个项目
java·ide·intellij-idea
HoneyMoose8 分钟前
IDEA 2024.3 版本更新主要功能介绍
java·ide·intellij-idea
我只会发热9 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
是老余11 分钟前
本地可运行,jar包运行错误【解决实例】:通过IDEA的maven package打包多模块项目
java·maven·intellij-idea·jar
crazy_wsp11 分钟前
IDEA怎么定位java类所用maven依赖版本及引用位置
java·maven·intellij-idea
.Ayang13 分钟前
tomcat 后台部署 war 包 getshell
java·计算机网络·安全·web安全·网络安全·tomcat·网络攻击模型
一直学习永不止步19 分钟前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表
hummhumm33 分钟前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j