JSP instance analysis for XML file operation skills, xml instance analysis

Source: Internet
Author: User

JSP instance analysis for XML file operation skills, xml instance analysis

This example describes JSP's XML file operations. We will share this with you for your reference. The details are as follows:

XML (Extensible Markup Language) Extensible Markup Language, which has been learned earlier. In this tutorial, why do we put it below J2EE? Because it is also one of the J2EE 13 specifications, although XML is specified by the W3C World Wide Web Organization Alliance and has been standardized, therefore, it is also a specification, and we must follow the same specifications as JDBC, Servlet, Jsp, and Ejb.

The front-end article has introduced some basic knowledge about XML, mainly used to store and transmit data. But how can we get the data? That is to say, how can we Parse XML? Here is a summary.

First, there are two ways to parse XML files: DOM parsing and SAX parsing. First, let's take a look at the concepts and basics of the two:

I. Principle:

DOM parsing: when the program starts execution, the entire XML file is first loaded into the memory to form a DOM tree in the memory, then, any node on the tree is added, deleted, modified, and queried using a programming language.
SAX parsing: event-driven parsing. Resolution is ordered, and the order follows: from left to right, from top to bottom.
The event-driven parsing method does not need to load all XML files into the memory, so this method does not consume a large amount of memory,
However, the nodes in the past cannot be parsed again, which is not flexible enough. If you want to parse the nodes, you can only start from the XML file header again.

Ii. Advantages and disadvantages of the two:

DOM parsing: flexible. Because the entire tree is in the memory, we can operate on a node anytime, anywhere, and the parsed nodes can be parsed again, which is more flexible.

Disadvantage: 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.
Conclusion: The XML file is small and there are many nodes to be parsed, so DOM resolution is worth using.

SAX parsing: advantages: it does not consume a large amount of memory.
Disadvantages: not flexible (we can use another technology, XPATH, which can be used to quickly locate
Resolved nodes ).
Conclusion: With the XPath technology, the SAX parsing method has become a common one.

3. in JAVA, the java JDK provides XML Parsing: Org. w3c. dom. *, which is SUN's implementation of w3c specifications. However, the efficiency is not high enough. Therefore, we often use third-party components, such as dom4j, with relatively higher efficiency.

4. Good. Let's take a look at the instance:

1. Use the JDK in JAVA to parse and read the XML file: Let's take a look at the details of the two blogs: Java Dom parsing and Java Sax parsing.

2. Write down here. The Parsing Method Using dom4j is actually very similar, but I feel that the method name of dom4j may be easier to use:

A. Read XML files (the following is based on the SAX Parsing Method ):

Public static void main (String [] args) throws Exception {// create a SAX Parser object SAXReader reader = new SAXReader (); // read the XML file Document document = reader. read (new File ("db-config.xml"); // gets the root Element rootElement = document. getRootElement (); System. out. println ("root node name:" + rootElement. getName (); // obtain the driver Element driverElement = rootElement of the child node under the root node. element ("driver"); String driver = driverElement. getText (); System. out. println (driver); // obtain the url of the child node under the root node String url = rootElement. elementText ("url"); System. out. println (url); // obtain the sub-node user String user = rootElement under the root node. elementText ("user"); System. out. println (user); // obtain the password of the subnode under the root node String password = rootElement. elementText ("password"); System. out. println (password );}

B. Write the file from top to bottom. The DOM Parsing Method in dom4j is as follows:

Public static void main (String [] args) throws Exception {// create a Document object in the memory: document Document document = policenthelper. createDocument (); // construct the document tree Element stuInfoElement = document. addElement ("mathematics books"); Element stuElement1 = stuInfoElement. addElement ("book"); stuelement1.addattriement ("id", "110"); Element nameElement1 = stuElement1.addElement ("title"); nameElement1.setText ("near generation Algebra "); element stuElement2 = stuInfoElement. addElement (""); stuelement2.addattriement ("id", "120"); Element nameElement2 = stuElement2.addElement ("title"); nameElement2.setText ("Higher Algebra "); // set the character encoding method OutputFormat format = OutputFormat. createPrettyPrint (); format. setEncoding ("GB18030"); // start to write XMLWriter xmlWriter = new XMLWriter (new FileWriter ("students. xml "), format); xmlWriter. write (document); xmlWriter. close ();}

Last written file:

<? Xml version = "1.0" encoding = "GB18030"?> <Mathematics Major books> <book id = "110"> <book title> Zhang San </book title> </book> <book id = "110"> <book title> Zhang San </book title> </book> </Mathematics Major>

To sum up, for the simple summary of XML parsing, of course, there will be very complex XML files, we can try to write these to parse their files, of course, they can also be written by others. Here we only want to learn more and understand their nature.

I hope this article will help you with JSP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.