How to add, delete, modify and check xml files with ajax
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:
<?xml version="1.0" encoding="UTF-8"?><Students> <student id="2"> <name>ttt</name> <age>44</age> </student> <student id="3"> <name>linda2</name> <age>22</age> </student> <student id="4"> <name>linda3</name> <age>23</age> </student> <student id="5"> <name>jack</name> <age>2</age> </student> <student id="1"> <name>yyh1</name> <age>22</age> </student> </Students>
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 <list.getLength(); i++) { if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){ updStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode(); //对学生的name属性进行赋新值 updStudent.getElementsByTagName("name").item(i).getFirstChild().setNodeValue(newName); //对学生的age 属性赋新值 updStudent.getElementsByTagName("age").item(i).getFirstChild().setNodeValue(newAge); }else{ break; } } //找出根元素,将修改后的元素持久化到文件 Element root = doc.getDocumentElement(); transform(root); System.out.println(updStudent); } //删除学生信息 private static void delStudent(Document doc) { Scanner input = new Scanner(System.in); //输入你要删除的学生的 学号 System.out.println("请输入要删除学生的学号:"); String studentid = input.nextLine(); Element root = doc.getDocumentElement(); //将学生列成一个表,进行遍历,找对应学号的学生进行删除 NodeList list = doc.getElementsByTagName("student"); for(int i = 0; i < list.getLength(); i++) { if((studentid).equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){ Element delStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode(); root.removeChild(delStudent); break; }else { System.out.println("没有该学生"); break; } } //持久化到文件 transform(root); } //添加学生信息 private static void addStudent(Document doc) { // System.out.println(doc.getElementsByTagName("student").item(1).getAttributes().getNamedItem("id").getNodeValue()); Element root = doc.getDocumentElement(); //从控制台输入 Scanner input = new Scanner(System.in); System.out.println("请输入学生的序号:id = "); //将学生放到一个列表里面,看我们要添加的学生的学号里面是否已经有了,如果有,需要将新加入的学生的学号改一下 NodeList list = doc.getElementsByTagName("student"); String studentid = input.nextLine(); for(int i = 0; i < list.getLength(); i++) { if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){ System.out.println("该序号学生表里面已经存在,请重新输入一个新的序号:"); studentid = input.nextLine(); }else { break; } } System.out.println("请输入要添加学生的姓名:name = "); String name_value = input.nextLine(); System.out.println("请输入要添加学生的年龄:age = "); String age_value = input.nextLine(); //创建节点 Element student = doc.createElement("student"); Element name = doc.createElement("name"); Element age = doc.createElement("age"); Text namText = doc.createTextNode(name_value); Text ageText = doc.createTextNode(age_value); //关联节点之间的关系 root.appendChild(student); student.appendChild(name); student.appendChild(age); student.setAttribute("id", studentid); name.appendChild(namText); age.appendChild(ageText); //持久化到文件 transform(root); } //持久化到文件的方法 private static void transform(Element root) throws TransformerFactoryConfigurationError { TransformerFactory factory = TransformerFactory.newInstance(); try { Transformer tf = factory.newTransformer(); tf.transform(new DOMSource(root), new StreamResult(new File("src/com/briup/dom/student.xml"))); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } }
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; } }
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 dataHow Ajax+Struts2 implements user input verification Code verification functionThe 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.

Ajax is not a specific version, but a technology that uses a collection of technologies to asynchronously load and update web page content. Ajax does not have a specific version number, but there are some variations or extensions of ajax: 1. jQuery AJAX; 2. Axios; 3. Fetch API; 4. JSONP; 5. XMLHttpRequest Level 2; 6. WebSockets; 7. Server-Sent Events; 8, GraphQL, etc.
