亚洲必赢app将xml文件作为二个小的数据库,举行学生的增加和删除改查,xml增加和删除

 1 1、查
 2     输入:www.oldboy.org
 3     获取当前backend下的所有记录
 4 
 5 2、新建
 6     输入:
 7         arg = {
 8             'backend': 'www.oldboy.org',
 9             'record':{
10                 'server': '100.1.7.9',
11                 'weight': 20,
12                 'maxconn': 30
13             }
14         }
15 
16 3、删除
17     输入:
18         arg = {
19             'backend': 'www.oldboy.org',
20             'record':{
21                 'server': '100.1.7.9',
22                 'weight': 20,
23                 'maxconn': 30
24             }
25         }
26 
27 复制代码

将xml文件作为三个小的数据库,进行学生的增加和删除改查,xml增加和删除

1.xml文件:

亚洲必赢app 1

 1 <?xml version="1.0" encoding="UTF-8"?><Students>
 2   <student id="2">
 3     <name>ttt</name>
 4     <age>44</age>
 5   </student>
 6   <student id="3">
 7     <name>linda2</name>
 8     <age>22</age>
 9   </student>
10   <student id="4">
11     <name>linda3</name>
12     <age>23</age>
13   </student>
14   <student id="5">
15     <name>jack</name>
16     <age>2</age>
17   </student>
18   <student id="1">
19       <name>yyh1</name>
20       <age>22</age>
21   </student>
22 </Students>

亚洲必赢app 2

 

2.Java代码

亚洲必赢app 3

  1 import java.io.File;
  2 import java.io.IOException;
  3 import java.util.Scanner;
  4 
  5 import javax.xml.parsers.ParserConfigurationException;
  6 import javax.xml.transform.Transformer;
  7 import javax.xml.transform.TransformerConfigurationException;
  8 import javax.xml.transform.TransformerException;
  9 import javax.xml.transform.TransformerFactory;
 10 import javax.xml.transform.TransformerFactoryConfigurationError;
 11 import javax.xml.transform.dom.DOMSource;
 12 import javax.xml.transform.stream.StreamResult;
 13 
 14 import org.w3c.dom.Document;
 15 import org.w3c.dom.Element;
 16 import org.w3c.dom.NodeList;
 17 import org.w3c.dom.Text;
 18 import org.xml.sax.SAXException;
 19 
 20 //在学生管理系统里面,学生的学号是唯一的,姓名有可能重复
 21 public class StudentManager {
 22     public static void main(String[] args) {
 23         try {
 24             Document doc = Domutils.getDoc(new File("xml文件的相对路径"));
 25             Scanner input = new Scanner(System.in);
 26             System.out.println("欢迎来到学生管理系统\n\n\n请输入你要进行什么操作是:\n1.添加学生信息\n2.删除学生信息\n3.修改学生信息\n(请输入前边的序号)");
 27             int num = input.nextInt();
 28             if(num == 1) {
 29                 addStudent(doc);
 30             }else if(num == 2) {
 31                 delStudent(doc);
 32             }else if(num == 3) {
 33                 updStudent(doc);
 34             }
 35         } catch (SAXException e) {
 36             e.printStackTrace();
 37         } catch (IOException e) {
 38             e.printStackTrace();
 39         } catch (ParserConfigurationException e) {
 40             e.printStackTrace();
 41         }
 42     }
 43     //修改学生信息
 44     private static void updStudent(Document doc) {
 45         Element updStudent = null;
 46         Scanner input = new Scanner(System.in);
 47         System.out.println("请输入你要修改的学生的学号:");
 48         String studentid = input.nextLine();
 49         System.out.println("请输入新学生的姓名:");
 50         String newName = input.nextLine();
 51         System.out.println("请输入新学生的年龄:");
 52         String newAge = input.nextLine();
 53         
 54         //将每一个学生的列出来,for循环判断你要修改信息的学生是哪一个
 55         NodeList list = doc.getElementsByTagName("student");
 56         for(int i = 0; i <list.getLength(); i++) {
 57             if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
 58                 updStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode();
 59                 //对学生的name属性进行赋新值
 60                 updStudent.getElementsByTagName("name").item(i).getFirstChild().setNodeValue(newName);
 61                 //对学生的age 属性赋新值
 62                 updStudent.getElementsByTagName("age").item(i).getFirstChild().setNodeValue(newAge);
 63                 
 64             }else{
 65                 break;
 66             }
 67         }
 68         //找出根元素,将修改后的元素持久化到文件
 69         Element root = doc.getDocumentElement();
 70         transform(root);
 71         System.out.println(updStudent);
 72     }
 73     //删除学生信息
 74     private static void delStudent(Document doc) {
 75         Scanner input = new Scanner(System.in);
 76         //输入你要删除的学生的 学号
 77         System.out.println("请输入要删除学生的学号:");
 78         String studentid = input.nextLine();
 79         Element root = doc.getDocumentElement();
 80         
 81         //将学生列成一个表,进行遍历,找对应学号的学生进行删除
 82         NodeList list = doc.getElementsByTagName("student");
 83         for(int i = 0; i < list.getLength(); i++) {
 84         if((studentid).equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
 85             Element delStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode(); 
 87                 root.removeChild(delStudent);
 88                 break;
 89             }else {
 90                 System.out.println("没有该学生");
 91                 break;
 92             }
 93         }
 94         //持久化到文件
 95         transform(root);
 96     }
 97     
 98     //添加学生信息
 99     private static void addStudent(Document doc) {
100 //        System.out.println(doc.getElementsByTagName("student").item(1).getAttributes().getNamedItem("id").getNodeValue());
101         Element root = doc.getDocumentElement();
102         //从控制台输入
103         Scanner input = new Scanner(System.in);
104         System.out.println("请输入学生的序号:id = ");
105          
106         //将学生放到一个列表里面,看我们要添加的学生的学号里面是否已经有了,如果有,需要将新加入的学生的学号改一下
107         NodeList list = doc.getElementsByTagName("student");
108         String studentid = input.nextLine();
109         for(int i = 0; i < list.getLength(); i++) {
110             if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
111                 System.out.println("该序号学生表里面已经存在,请重新输入一个新的序号:");
112                  studentid = input.nextLine();
113             }else {
114                 break;
115             }
116         }
117         
118         System.out.println("请输入要添加学生的姓名:name = ");
119         String name_value = input.nextLine();
120         System.out.println("请输入要添加学生的年龄:age = ");
121         String age_value = input.nextLine();
122         
123         //创建节点
124         Element student = doc.createElement("student");
125         Element name = doc.createElement("name");
126         Element age = doc.createElement("age");
127         Text namText = doc.createTextNode(name_value);
128         Text ageText = doc.createTextNode(age_value);
129         //关联节点之间的关系
130         root.appendChild(student);
131         student.appendChild(name);
132         student.appendChild(age);
133         student.setAttribute("id", studentid);
134         name.appendChild(namText);
135         age.appendChild(ageText);
136         //持久化到文件
137         transform(root);
138         
139     }
140     //持久化到文件的方法
141     private static void transform(Element root)
142             throws TransformerFactoryConfigurationError {
143         TransformerFactory factory = TransformerFactory.newInstance();
144         try {
145             Transformer tf = factory.newTransformer();
146             tf.transform(new DOMSource(root), new StreamResult(new File("src/com/briup/dom/student.xml")));
147         } catch (TransformerConfigurationException e) {
148             e.printStackTrace();
149         } catch (TransformerException e) {
150             e.printStackTrace();
151         }
152     }
153 }

亚洲必赢app 4

2.Dom剖判文件(将赢得分析文件的某些包装起来)

亚洲必赢app 5

import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.AclEntry.Builder;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Domutils {
    public static Document getDoc(File file) throws SAXException, IOException, ParserConfigurationException {
      //获取工厂模式
        DocumentBuilderFactory factory = 
                DocumentBuilderFactory.newInstance();
        //获取builder对象
            DocumentBuilder builder = factory.newDocumentBuilder();   
        //将要解析文件加载成一个树状文件,开始解析         
            Document document = builder.parse(file);
        return document;
    }
}

亚洲必赢app 6

 

http://www.bkjia.com/Javabc/1135482.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1135482.htmlTechArticle将xml文件作为一个小的数据库,进行学生的增删改查,xml增删
1.xml文件: 1 ? xml version=”1.0″ encoding=”UTF-8″ ? Students 2 student
id =”2″ 3 name ttt / na…

亚洲必赢app 7亚洲必赢app 8

1 def create_web_info(x):
2     key_list=[]
3     key1_list=[]
4     with open("haproxy_file",'a') as f:
5         f.write('bakend'+' '+x["bakend"]+'\n')
6         web_info_dic=x['record']
7         f.write('\t\t'+'server'+' '+web_info_dic['server']+' '+'weight'+' '\
8             +str(web_info_dic['weight'])+' '+'maxconn'+' '+str(web_info_dic['maxconn'])+'\n')
9     print("写入后端信息成功")

亚洲必赢app 9亚洲必赢app 10

 1 global       
 2         log 127.0.0.1 local2
 3         daemon
 4         maxconn 256
 5         log 127.0.0.1 local2 info
 6 defaults
 7         log global
 8         mode http
 9         timeout connect 5000ms
10         timeout client 50000ms
11         timeout server 50000ms
12         option  dontlognull
13 
14 listen stats :8888
15         stats enable
16         stats uri       /admin
17         stats auth      admin:1234
18 
19 frontend oldboy.org
20         bind 0.0.0.0:80
21         option httplog
22         option httpclose
23         option  forwardfor
24         log global
25         acl www hdr_reg(host) -i www.oldboy.org
26         use_backend www.oldboy.org if www
27 
28 backend www.oldboy.org
29         server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000

亚洲必赢app 11亚洲必赢app 12

 

亚洲必赢app 13亚洲必赢app 14

一,程序要求

 1 while True:
 2     print('''
 3             welecome to haproxy file           
 4                 1.find_web_info            
 5                 2.create_web_info
 6                 3.delete_web_ifo   
 7         ''')
 8     choice_num=int(input("输入你对haproxy文件的操作选择:"))
 9     if choice_num ==1:
10         find_name=input("输入需要查找的域名:")
11         find_web_info(find_name )
12     elif choice_num ==2:
13         create_info=input("输入你插入的后端信息:")
14         create_info =eval(create_info )
15         create_web_info(create_info)
16     elif choice_num ==3:
17         delete_info=input("输入你删除的信息:")
18         delete_info =eval(delete_info )
19         delete_web_info(delete_info )
20     else:
21         print("输入有误,请再输入一次")

创造音讯模块

亚洲必赢app 15亚洲必赢app 16

haproxy配置文件

 

研究音信模块

输入以上音信能够直达需求的意义

 1 def find_web_info(x):
 2         if choice_num==1:
 3             with open("haproxy_file",'r') as f:
 4                 for line in f:
 5                     if line.startswith("backend %s" %x):
 6                         for i in range(10000):
 7                             read=f.readline()
 8                             if read.startswith('backend'):
 9                                 break
10                             elif read.strip()=='':
11                                 pass
12                             else:
13                                 print(read)
14                     else:
15                         pass

主函数模块

除去信息模块

亚洲必赢app 17亚洲必赢app 18

程序须要

 1 def delete_web_info(x):
 2     find_line=''
 3     backend_name='bakend'+' '+x["bakend"]
 4     backend_name=backend_name.strip()
 5     web_info_dic=x['record']
 6     back_info='server'+' '+web_info_dic['server']+' '+'weight'+' '\
 7             +str(web_info_dic['weight'])+' '+'maxconn'+' '+str(web_info_dic['maxconn'])
 8     back_info=back_info.strip()
 9     with open("haproxy_file",'r') as f:
10         for line in f:
11             if backend_name in line:
12                line=line.replace(line,'')
13             elif back_info in line:
14                line=line.replace(line,'')
15             else:
16                 pass
17             find_line+=line
18     with open("haproxy_file",'w') as f1:
19         f1.write(find_line )
20     print("后端信息删除完成")

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图