题目:基于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() {
    }
}
相关推荐
pk_xz12345618 分钟前
Shell 脚本中变量和字符串的入门介绍
linux·运维·服务器
小珑也要变强20 分钟前
Linux之sed命令详解
linux·运维·服务器
海绵波波10727 分钟前
Webserver(4.3)TCP通信实现
服务器·网络·tcp/ip
阿伟*rui1 小时前
配置管理,雪崩问题分析,sentinel的使用
java·spring boot·sentinel
九河云2 小时前
AWS账号注册费用详解:新用户是否需要付费?
服务器·云计算·aws
Lary_Rock2 小时前
RK3576 LINUX RKNN SDK 测试
linux·运维·服务器
幺零九零零3 小时前
【计算机网络】TCP协议面试常考(一)
服务器·tcp/ip·计算机网络
XiaoLeisj3 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
paopaokaka_luck3 小时前
【360】基于springboot的志愿服务管理系统
java·spring boot·后端·spring·毕业设计
dayouziei3 小时前
java的类加载机制的学习
java·学习