Home > Web Front-end > JS Tutorial > body text

How to add, delete, modify and check xml files with ajax

php中世界最好的语言
Release: 2018-04-04 11:38:35
Original
2014 people have browsed it

This time I will show you how ajax can add, delete, modify, and check xml files. What are the precautions for adding, deleting, modifying, and checking xml files with ajax? The following is a practical case. Let's take a look. 1.xml file:


 
  ttt
  44
 
 
  linda2
  22
 
 
  linda3
  23
 
 
  jack
  2
 
 
   yyh1
   22
 
Copy after login

2.Java code

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
//在学生管理系统里面,学生的学号是唯一的,姓名有可能重复
public class StudentManager {
  public static void main(String[] args) {
    try {
      Document doc = Domutils.getDoc(new File("xml文件的相对路径"));
      Scanner input = new Scanner(System.in);
      System.out.println("欢迎来到学生管理系统\n\n\n请输入你要进行什么操作是:\n1.添加学生信息\n2.删除学生信息\n3.修改学生信息\n(请输入前边的序号)");
      int num = input.nextInt();
      if(num == 1) {
        addStudent(doc);
      }else if(num == 2) {
        delStudent(doc);
      }else if(num == 3) {
        updStudent(doc);
      }
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    }
  }
  //修改学生信息
  private static void updStudent(Document doc) {
    Element updStudent = null;
    Scanner input = new Scanner(System.in);
    System.out.println("请输入你要修改的学生的学号:");
    String studentid = input.nextLine();
    System.out.println("请输入新学生的姓名:");
    String newName = input.nextLine();
    System.out.println("请输入新学生的年龄:");
    String newAge = input.nextLine();
    
    //将每一个学生的列出来,for循环判断你要修改信息的学生是哪一个
    NodeList list = doc.getElementsByTagName("student");
    for(int i = 0; i 
Copy after login

2.Dom parsing file (encapsulate the part that gets the parsed file)

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;
  }
}
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Ajax restful interface method of transmitting Json data


How Ajax+Struts2 implements user input verification Code verification function

The above is the detailed content of How to add, delete, modify and check xml files with ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!