热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

69期JavaSE029_XML解析

###综合练习使用Socket和多线程编写一个简单的聊天小程序,要求客户端和服务端交替发送消息,在客户端和服务端都能看到彼此的聊天记录。Serverja

 

### 综合练习使用 Socket 和多线程编写一个简单的聊天小程序,要求客户端和服务端交替发送消息,在客户端和服务端都能看到彼此的聊天记录。Server```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;try {serverSocket = new ServerSocket(8080);System.out.println("服务端已启动");while(true) {socket = serverSocket.accept();new Thread(new SocketThread(socket)).start();;}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}
```ServerThread```java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;public class SocketThread implements Runnable {private Socket socket;public SocketThread(Socket socket) {this.socket = socket;}@Overridepublic void run() {// TODO Auto-generated method stubInputStream inputStream = null;DataInputStream dataInputStream = null;Scanner scanner = new Scanner(System.in);String message = null;OutputStream outputStream = null;DataOutputStream dataOutputStream = null;try {while(true) {//inputStream = socket.getInputStream();dataInputStream = new DataInputStream(inputStream);message = dataInputStream.readUTF();System.out.println("客户端:"+message);//System.out.print("服务器:");message = scanner.next();outputStream = socket.getOutputStream();dataOutputStream = new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataOutputStream.close();outputStream.close();dataInputStream.close();inputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}
}
```Client```java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;public class Client {public static void main(String[] args) {Socket socket = null;InputStream inputStream = null;DataInputStream dataInputStream = null;OutputStream outputStream = null;DataOutputStream dataOutputStream = null;System.out.println("客户端已启动...");try {socket = new Socket("127.0.0.1", 8080);Scanner scanner = new Scanner(System.in);String message = null;while(true) {//System.out.print("客户端:");message = scanner.next();outputStream = socket.getOutputStream();dataOutputStream = new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);//inputStream = socket.getInputStream();dataInputStream = new DataInputStream(inputStream);message = dataInputStream.readUTF();System.out.println("服务器:"+message);}} catch (UnknownHostException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataInputStream.close();inputStream.close();dataOutputStream.close();outputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}
}
```### XML 解析XML:Extensible Markup Language 可扩展标记语言特点:XML 与操作系统无关、与编程语言无关,可以实现不同系统之间的数据交换。XML 文档内容由一系列标签元素组成
<标签名 属性名&#61;"属性值">元素内容- 属性值用双引号包裹
- 一个元素可以包含多个属性
- 属性值中不能直接包含<、>、"、&、&#39;

&#96;&#96;&#96;xml

张三Java高级编程Java是一门面向对象高级编程语言

&#96;&#96;&#96;注意事项
- 所有 XML 元素都必须有结束标签
- XML 标签对大小写敏感
- XML 必须有正确的嵌套
- 同级标签以缩进对齐
- 元素名称可以包含字母、数字或其他字符
- 元素名称不能以数字或标点符号开头
- 元素名称不能包含空格XML 的转义字符特殊字符&#xff1a;<> " &#39; &

转义符&#xff1a;&#96;&#96;&#96;
<> " ' &amp;
&#96;&#96;&#96;衣服型号 对应身高S
<165M 165-170L 170-175XL 175-180XXL 180-185&#96;&#96;&#96;xml

165">SMLXLXXL

&#96;&#96;&#96;考生姓名 预计得分 实际得分张三
75 80李四 75 88&#96;&#96;&#96;xml



&#96;&#96;&#96;### 解析XML用 Java 程序读取、操作 XML 文件中的数据dom4j 主流的 Java XML API&#xff0c;性能优异&#xff0c;功能强大。&#96;&#96;&#96;xml



&#96;&#96;&#96;&#96;&#96;&#96;java
import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class Test {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");//获取根节点Element root &#61; document.getRootElement();Iterator rootIter &#61; root.elementIterator();while(rootIter.hasNext()) {Element ele &#61; rootIter.next();String name &#61; ele.attributeValue("name");System.out.println(name);Iterator eleIter &#61; ele.elementIterator();while(eleIter.hasNext()) {Element type &#61; eleIter.next();String typeName &#61; type.attributeValue("name");System.out.println(typeName);}}} catch (DocumentException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}
&#96;&#96;&#96;&#96;&#96;&#96;java
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;public class Test2 {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");Element root &#61; document.getRootElement();Element newEle &#61; root.addElement("brand");newEle.addAttribute("name", "Vivo");Element type &#61; newEle.addElement("type");type.addAttribute("name", "型号1");OutputFormat format &#61; OutputFormat.createPrettyPrint();format.setEncoding("UTF-8");Writer fileWriter &#61; new FileWriter("src/phone.xml");XMLWriter writer &#61; new XMLWriter(fileWriter,format);writer.write(document);writer.close();} catch (DocumentException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}
&#96;&#96;&#96;

Client.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;public class Client {public static void main(String[] args) {Socket socket &#61; null;InputStream inputStream &#61; null;DataInputStream dataInputStream &#61; null;OutputStream outputStream &#61; null;DataOutputStream dataOutputStream &#61; null;System.out.println("客户端已启动...");try {socket &#61; new Socket("127.0.0.1", 8080);Scanner scanner &#61; new Scanner(System.in);String message &#61; null;while(true) {//System.out.print("客户端&#xff1a;");message &#61; scanner.next();outputStream &#61; socket.getOutputStream();dataOutputStream &#61; new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);//inputStream &#61; socket.getInputStream();dataInputStream &#61; new DataInputStream(inputStream);message &#61; dataInputStream.readUTF();System.out.println("服务器&#xff1a;"&#43;message);}} catch (UnknownHostException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataInputStream.close();inputStream.close();dataOutputStream.close();outputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}
}

 

cloth.xml

xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<cloth><size range&#61;"height>165">Ssize><size range&#61;"165>Msize><size range&#61;"170>Lsize><size range&#61;"175>XLsize><size range&#61;"180>XXLsize>
cloth>

 

phone.xml

xml version&#61;"1.0" encoding&#61;"UTF-8"?><phoneinfo id&#61;"1"> <brand name&#61;"华为"> <type name&#61;"U8650"/> <type name&#61;"HW123"/> <type name&#61;"HW321"/> brand> <brand name&#61;"苹果"> <type name&#61;"iPhone"/> brand> <brand name&#61;"三星"> <type name&#61;"Note4"/> brand> <brand name&#61;"Vivo"/> <brand name&#61;"Vivo"><type name&#61;"型号1"/>brand>
phoneinfo>

 

score.xml

xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<students><student name&#61;"张三" expected_score&#61;"75" score&#61;"80">student><student name&#61;"李四" expected_score&#61;"75" score&#61;"88">student>
students>

 

Server.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class Server {public static void main(String[] args) {ServerSocket serverSocket &#61; null;Socket socket &#61; null;try {serverSocket &#61; new ServerSocket(8080);System.out.println("服务端已启动");while(true) {socket &#61; serverSocket.accept();new Thread(new SocketThread(socket)).start();;}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}

 

SocketThread.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;public class SocketThread implements Runnable {private Socket socket;public SocketThread(Socket socket) {this.socket &#61; socket;}&#64;Overridepublic void run() {// TODO Auto-generated method stubInputStream inputStream &#61; null;DataInputStream dataInputStream &#61; null;Scanner scanner &#61; new Scanner(System.in);String message &#61; null;OutputStream outputStream &#61; null;DataOutputStream dataOutputStream &#61; null;try {while(true) {//inputStream &#61; socket.getInputStream();dataInputStream &#61; new DataInputStream(inputStream);message &#61; dataInputStream.readUTF();System.out.println("客户端&#xff1a;"&#43;message);//System.out.print("服务器&#xff1a;");message &#61; scanner.next();outputStream &#61; socket.getOutputStream();dataOutputStream &#61; new DataOutputStream(outputStream);dataOutputStream.writeUTF(message);}} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}finally {try {dataOutputStream.close();outputStream.close();dataInputStream.close();inputStream.close();} catch (IOException e) {// TODO Auto-generated catch block
e.printStackTrace();}}}}

 

Test.java

import java.util.Iterator;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class Test {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");//获取根节点Element root &#61; document.getRootElement();Iterator rootIter &#61; root.elementIterator();while(rootIter.hasNext()) {Element ele &#61; rootIter.next();String name &#61; ele.attributeValue("name");System.out.println(name);Iterator eleIter &#61; ele.elementIterator();while(eleIter.hasNext()) {Element type &#61; eleIter.next();String typeName &#61; type.attributeValue("name");System.out.println(typeName);}}} catch (DocumentException e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}

 

test.xml

xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<books><book id&#61;"001"><author>张三author><name>Java高级编程name><description>Java是一门面向对象高级编程语言description>book><book id&#61;"002" author&#61;"李四" name&#61;"C#" description&#61;"面向对象">book>
books>

 

Test2.java

import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;public class Test2 {public static void main(String[] args) {SAXReader saxReader &#61; new SAXReader();try {Document document &#61; saxReader.read("src/phone.xml");Element root &#61; document.getRootElement();Element newEle &#61; root.addElement("brand");newEle.addAttribute("name", "Vivo");Element type &#61; newEle.addElement("type");type.addAttribute("name", "型号1");OutputFormat format &#61; OutputFormat.createPrettyPrint();format.setEncoding("UTF-8");Writer fileWriter &#61; new FileWriter("src/phone.xml");XMLWriter writer &#61; new XMLWriter(fileWriter,format);writer.write(document);writer.close();} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}

 

转:https://www.cnblogs.com/HiJackykun/p/11173474.html



推荐阅读
  • 本文基于Java官方文档进行了适当修改,旨在介绍如何实现一个能够同时处理多个客户端请求的服务端程序。在前文中,我们探讨了单客户端访问的服务端实现,而本篇将深入讲解多客户端环境下的服务端设计与实现。 ... [详细]
  • Asynchronous JavaScript and XML (AJAX) 的流行很大程度上得益于 Google 在其产品如 Google Suggest 和 Google Maps 中的应用。本文将深入探讨 AJAX 在 .NET 环境下的工作原理及其实现方法。 ... [详细]
  • 本文探讨了使用Python实现监控信息收集的方法,涵盖从基础的日志记录到复杂的系统运维解决方案,旨在帮助开发者和运维人员提升工作效率。 ... [详细]
  • Java虚拟机及其发展历程
    Java虚拟机(JVM)是每个Java开发者日常工作中不可或缺的一部分,但其背后的运作机制却往往显得神秘莫测。本文将探讨Java及其虚拟机的发展历程,帮助读者深入了解这一关键技术。 ... [详细]
  • 本文介绍了Tomcat的基本操作,包括启动、关闭及首次访问的方法,并详细讲解了如何在IDEA中创建Web项目,配置Servlet及其映射,以及如何将项目部署到Tomcat。 ... [详细]
  • 笔记说明重学前端是程劭非(winter)【前手机淘宝前端负责人】在极客时间开的一个专栏,每天10分钟,重构你的前端知识体系& ... [详细]
  • WebBenchmark:强大的Web API性能测试工具
    本文介绍了一款名为WebBenchmark的Web API性能测试工具,该工具不仅支持HTTP和HTTPS服务的测试,还提供了丰富的功能来帮助开发者进行高效的性能评估。 ... [详细]
  • Spring Security基础配置详解
    本文详细介绍了Spring Security的基础配置方法,包括如何搭建Maven多模块工程以及具体的安全配置步骤,帮助开发者更好地理解和应用这一强大的安全框架。 ... [详细]
  • 探讨了在HTML表单中使用元素代替进行表单提交的方法。 ... [详细]
  • Hibernate全自动全映射ORM框架,旨在消除sql,是一个持久层的ORM框架1)、基础概念DAO(DataAccessorOb ... [详细]
  • 本文探讨了如何利用RxJS库在AngularJS应用中实现对用户单击和拖动操作的精确区分,特别是在调整区域大小的场景下。 ... [详细]
  • Android 中的布局方式之线性布局
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文探讨了如何在PHP与MySQL环境中实现高效的分页查询,包括基本的分页实现、性能优化技巧以及高级的分页策略。 ... [详细]
  • 处理Android EditText中数字输入与parseInt方法
    本文探讨了如何在Android应用中从EditText组件安全地获取并解析用户输入的数字,特别是用于设置端口号的情况。通过示例代码和异常处理策略,展示了有效的方法来避免因非法输入导致的应用崩溃。 ... [详细]
  • Maven + Spring + MyBatis + MySQL 环境搭建与实例解析
    本文详细介绍如何使用MySQL数据库进行环境搭建,包括创建数据库表并插入示例数据。随后,逐步指导如何配置Maven项目,整合Spring框架与MyBatis,实现高效的数据访问。 ... [详细]
author-avatar
幸福的小兔子3
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有