Example analysis of JSP's operation skills for XML files

  • 2021-08-17 00:40:08
  • OfStack

This paper describes the operation skills of JSP for XML files. Share it for your reference, as follows:

XML (Extensible Markup Language) Extensible Markup Language, the basics of which have been learned earlier. And why does this tutorial put it under J2EE, because it is also the first of the 13 specifications of J2EE. Although XML is specified and standardized by W3C World Wide Web Organization Alliance, it is also a specification, and we must follow the specifications, such as JDBC, Servlet, Jsp and Ejb.

The previous article has introduced some basic knowledge of XML, which is mainly used to store and transmit data, but how do we get these data, that is to say, how to analyze XML? To sum up here.

First of all, there are two ways to analyze XML file 1: DOM analysis and SAX analysis. First, look at the concepts and basic knowledge of the two:

1. Principle:

DOM parsing: When the program starts to execute, first load the whole XML file into memory, form an DOM tree in memory, and then add, delete, modify and check any node in this tree through a certain programming language.
SAX parsing: Based on event-driven parsing. Parsing is sequential, and the order is observed: from left to right, from top to bottom.
Event-driven parsing does not require all XML files to be loaded into memory, so this method does not consume a lot of memory.
Only parsing the past nodes can not be parsed again, which is not flexible enough. If you still want to parse, you can only start from the XML file header again.

2. Advantages and disadvantages of the two:

DOM parsing: Advantages: Flexibility. Because the whole tree is in memory, we can operate a certain node anytime and anywhere, and we can analyze the past nodes again, which is more flexible.

Disadvantages: If the XML file is large, it will consume a lot of memory, because the XML file is large and needs to be accessed
There are very few nodes of.
Summary: Therefore, the XML file is small and there are many nodes to be parsed, which makes it worth using DOM parsing.

SAX parsing: Advantages: It does not consume a lot of memory.
Disadvantages: Not flexible (we can use another technology, XPATH, which can quickly locate the key in the XML file
Parsed node).
Summary: With XPath technology, SAX parsing has become commonly used by us.

3. In JAVA, JDK of java provides an analysis of XML: org.w3c.dom.*, which is the implementation of w3c specification by SUN. But the efficiency is not high enough. Therefore, we often use third-party components, such as dom4j, etc., which are relatively efficient.

4. OK, let's look at an example:

1. Use JDK in JAVA to read XML file: Look at two blogs written in detail: Java Dom analysis, Java Sax analysis.

2. Write 1 here. The parsing method of dom4j is actually very similar, but I feel that the method name and attribute name of dom4j may be easier to use:

a, read the XML file (the following is based on SAX parsing):


public static void main(String[] args) throws Exception{ 
    // Create SAX Parser object  
    SAXReader reader = new SAXReader(); 
    // Read XML Documents  
    Document document = reader.read(new File("db-config.xml")); 
    // Get the root element  
    Element rootElement = document.getRootElement(); 
    System.out.println(" The name of the root node: " + rootElement.getName()); 
    // Get the child nodes under the root node driver 
    Element driverElement = rootElement.element("driver"); 
    String driver = driverElement.getText(); 
    System.out.println(driver); 
    // Get the child nodes under the root node url 
    String url = rootElement.elementText("url"); 
    System.out.println(url); 
    // Get the child nodes under the root node user 
    String user = rootElement.elementText("user"); 
    System.out.println(user); 
    // Get the child nodes under the root node password 
    String password = rootElement.elementText("password"); 
    System.out.println(password); 
} 

b, writing files, is from top to bottom. The way of DOM parsing in dom4j:


public static void main(String[] args) throws Exception{ 
    // Create in memory first 1 Document objects  
    Document document = DocumentHelper.createDocument(); 
    // Construct a document tree  
    Element stuInfoElement = document.addElement(" Mathematics books "); 
    Element stuElement1 = stuInfoElement.addElement(" Books "); 
    stuElement1.addAttribute("id", "110"); 
    Element nameElement1 = stuElement1.addElement(" Book title "); 
    nameElement1.setText(" Modern algebra "); 
    Element stuElement2 = stuInfoElement.addElement(" Books "); 
    stuElement2.addAttribute("id", "120"); 
    Element nameElement2 = stuElement2.addElement(" Book title "); 
    nameElement2.setText(" Higher algebra "); 
    // Set the character encoding method  
    OutputFormat format = OutputFormat.createPrettyPrint(); 
    format.setEncoding("GB18030"); 
    // Start writing  
    XMLWriter xmlWriter = new XMLWriter(new FileWriter("students.xml"),format); 
    xmlWriter.write(document); 
    xmlWriter.close(); 
} 

The final written document:


<?xml version="1.0" encoding="GB18030"?> 
 < Mathematics books > 
  < Books  id="110"> 
    < Book title > Zhang 3</ Book title > 
  </ Books > 
 < Books  id="110"> 
    < Book title > Zhang 3</ Book title > 
  </ Books > 
 </ Mathematics books > 

To sum up, for the simple summary of XML parsing, of course, we will encounter very complicated XML files in practice. We can try to write these files to parse them ourselves, and of course we can also use others to write them. Here is just to learn more and understand their essence.

I hope this article is helpful to everyone's JSP programming.


Related articles: