package xml课程设计;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import org.dom4j.Attribute;
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 Ways {
private String namesString;
private String start_timeString;
private String end_timeString;
String strRegex1 = "[0-9]{1,3}[\u4e00-\u9fa5]";//定义公交车名命名规则
String strRegex2 = "([0-1][0-9]|2[0-3]):([0-5][0-9])";//定义时间规则
String strRegex3 = "[\u4e00-\u9fa5]{2,6}";//定义车站命名规则
public String getNamesString() {
return namesString;
}
public void setNamesString(String namesString) {
this.namesString = namesString;
}
public String getStart_timeString() {
return start_timeString;
}
public void setStart_timeString(String start_timeString) {
this.start_timeString = start_timeString;
}
public String getEnd_timeString() {
return end_timeString;
}
public void setEnd_timeString(String end_timeString) {
this.end_timeString = end_timeString;
}
/**
* 遍历所有公交线路的站点信息
*
* @param node
*/
public void checkxml(Element node) {
System.out.println(node.getName() + ":");
List<Attribute> bookAttr = node.attributes();// 获取根元素的属性名及属性值
for (Attribute attr : bookAttr) {
System.out.println(attr.getName() + ":" + attr.getValue());// 打印属性值
}
int count = 0;
Iterator itt = node.elementIterator();
while (itt.hasNext()) {
Element book = (Element) itt.next();
count++;
// 获取book的属性名以及 属性值
List<Attribute> bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
System.out.println(attr.getName() + ":" + attr.getValue());
}
Iterator<Element> it = book.elementIterator();
// 遍历
while (it.hasNext()) {
// 获取某个子节点对象
Element e = (Element) it.next();
// 对子节点进行遍历
System.out.println(e.getName() + ":" + e.getStringValue());
}
System.out.println("一共遍历" + count + "条信息");
System.out.println();
}
}
/**
* 查找一条公交线路的站点信息
*
* @param name
* @param node
* @return
*/
public boolean checkone(String name, Element node) {
boolean flag = false;
Iterator itt = node.elementIterator();
while (itt.hasNext()) {
Element book = (Element) itt.next();
// 获取book的属性名以及 属性值
List<Attribute> bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
if (attr.getValue().equals(name)) {
// 输出所符合的节点属性信息
Iterator its = book.attributeIterator();
while (its.hasNext()) {
Attribute attribute = (Attribute) its.next();
System.out.println(attribute.getName() + ":" + attribute.getValue());
}
flag = true;
Iterator<Element> it = book.elementIterator();
// 输出这个节点的所有子节点信息
while (it.hasNext()) {
// 获取某个子节点对象
Element e = (Element) it.next();
// 对子节点进行遍历
System.out.println(e.getName() + ":" + e.getStringValue());
}
}
}
}
System.out.println();
return flag;
}
/**
* 删除一条公交线路的站点信息
*
* @param nameString
* @param node
* @param document
* @return
* @throws IOException
*/
public boolean deletexml(String nameString, Element node, Document document) throws IOException {
boolean flag = false;
Iterator itt = node.elementIterator();
while (itt.hasNext()) {
Element book = (Element) itt.next();
// 获取book的属性名以及 属性值
List<Attribute> bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
if (attr.getValue().equals(nameString)) {
node.remove(book);
flag = true;
// 排版缩进的格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置编码
format.setEncoding("gb2312");
// 创建XMLWriter对象,指定了写出文件及编码格式
// XMLWriter writer = new XMLWriter(new FileWriter(new
// File("src//a.xml")),format);
XMLWriter writer = null;
try {
writer = new XMLWriter(
new OutputStreamWriter(new FileOutputStream(new File("src/bbb.xml")), "gb2312"),
format);
} catch (UnsupportedEncodingException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
// 写入
try {
writer.write(document);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// 立即写入
writer.flush();
// 关闭操作
writer.close();
}
}
}
System.out.println();
return flag;
}
/**
* 添加一条公交线路的站点信息
*
* @param node
* @param document
* @throws IOException
*/
public void insertxml(Element node, Document document) throws IOException {
boolean flag2 = true;
boolean flag3 = true;
Element newelElement = node.addElement("线路");
System.out.println("公交车名:");
Scanner sc = new Scanner(System.in);
String string1 = sc.nextLine();
Iterator itt = node.elementIterator();
while (itt.hasNext()) {
Element book = (Element) itt.next();
// 获取book的属性名以及 属性值
List<Attribute> bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
if (attr.getValue().equals(string1)) {
flag3 = false;
}
}
}
if (!flag3) {
System.out.println("该车已有信息!");
} else if (string1.matches(strRegex1)) {
newelElement.addAttribute("公交车", string1);
System.out.println("开始时间:");
String string2 = sc.next();
if (string2.matches(strRegex2)) {
newelElement.addAttribute("开始时间", string2);
System.out.println("结束时间:");
String string3 = sc.next();
if (string3.matches(strRegex2)) {
newelElement.addAttribute("结束时间", string3);
System.out.println("输入添加站点个数:");
int a;
while (true) {
Scanner input = new Scanner(System.in);
if (!input.hasNextInt()) {
System.out.println("输入数据有误,请重新输入一个整数");
} else {
a = input.nextInt();
break;
}
}
String kong = sc.nextLine();
for (int i = 0; i < a; i++) {
System.out.println("输入站点信息:");
String string = sc.nextLine();
if (string.matches(strRegex3)) {
Element e = newelElement.addElement("站点");
e.setText(string);
// 排版缩进的格式
} else {
System.out.println("输入的站点信息有误!(格式为:2-6个汉字)");
flag2 = false;
break;
}
}
} else {
System.out.println("输入结束时间格式有误!(格式为:xx:xx)");
flag2 = false;
}
} else {
System.out.println("输入开始时间格式有误!(格式为:xx:xx)");
flag2 = false;
}
} else {
System.out.println("输入公交线路有误!(格式为:xxx路)");
flag2 = false;
}
if (flag2 && flag3) {
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置编码
format.setEncoding("gb2312");
XMLWriter writer = null;
writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("src/bbb.xml")), "gb2312"),
format);
// 写入
writer.write(document);
// 立即写入
writer.flush();
// 关闭操作
writer.close();
System.out.println("添加成功!");
System.out.println();
}
}
/**
* 修改一条公交线路的发车时间
*
* @param nameString
* @param node
* @param document
* @return
* @throws IOException
*/
public boolean
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
XML公交站点信息的增删改查.zip (13个子文件)
XML公交站点信息的增删改查
XmlProject
bin
bbb.xml 1KB
xml课程设计
Demo.class 3KB
Ways.class 9KB
aaa.dtd 357B
.settings
org.eclipse.jdt.core.prefs 598B
src
bbb.xml 1KB
xml课程设计
Demo.java 3KB
Ways.java 9KB
aaa.dtd 357B
.project 383B
.classpath 455B
lib
dom4j-1.6.1.jar 307KB
文档.doc 249KB
共 13 条
- 1
资源评论
提笔画个猴
- 粉丝: 2
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功