题目:基于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() {
    }
}
相关推荐
hong_zc9 分钟前
JDBC 编程
java·数据库·mysql
Flying_Fish_roe10 分钟前
MyBatis-Plus 常见问题与优化
java·tomcat·mybatis
X² 编程说14 分钟前
14.面试算法-字符串常见算法题(三)
java·数据结构·后端·算法·面试
Tony聊跨境18 分钟前
Facebook运营:账号类型有哪些?有必要用静态住宅IP吗?
网络协议·tcp/ip·facebook
Michael的跨境生活大杂烩21 分钟前
Facebook直播限流是什么原因?是ip地址导致的吗
网络协议·tcp/ip·facebook
imc.111 小时前
初识linux(2)
java·linux·数据库
武子康1 小时前
大数据-143 - ClickHouse 集群 SQL 超详细实践记录!
java·大数据·数据库·分布式·sql·clickhouse·flink
巧手打字通1 小时前
解锁Java线程池:实战技巧与陷阱规避
java·性能优化·线程池
再拼一次吧1 小时前
Linux软件安装
linux·运维·服务器
装不满的克莱因瓶1 小时前
【微服务】Eureka的自我保护机制
java·spring cloud·云原生·eureka·注册中心·服务注册