(1)使用dom4j将信息存入xml中
(2)读取信息,并打印控制台
(3)添加一个city节点与子节点
(4)使用socket TCP协议编写服务端与客户端,
客户端输入城市ID,服务器响应相应城市信息
(5)使用socket TCP协议编写服务端与客户端,客户端要求用户输入city对象,服务端接收并使用dom4j保存至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>
第一题:
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();
}
}
}
第二题:
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();
}
}
}
第三题:
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();
}
}
}
第四题:
服务端:
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;
}
}
客户端:
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();
}
}
}
第五题:
客户端:
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);
}
}
}
服务端:
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对象:
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() {
}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 题目:基于TCP/IP用DOM4j修改XML
发表评论 取消回复