Introduction to javaSE learning notes Xpath and web server (8), export expath
Xpath is an independent path language mainly used to find elements in a path. DOM4J supports path search of Xpath by default. There are two common methods: selectNodes ("xpath") to query all the elements in the specified path selectSingleNode ("xpath ") query the first element of a specified path. Common Xpath path writing method/AAA: Specify the write path starting from the root of the XML document. // BBB: Specify to search for the expected element name/AAA/CCC from the entire XML file. /DDD/* specify all elements in the path // * specify to obtain all elements/AAA/BBB [1] specify the first element in the path, it must start with 1./AAA/BBB [last ()] specify the last element in the path // @ id specify the attribute element to be obtained // BBB [@ id] specify the element with the attribute // BBB [@ id = 'b1 '] element of the specified attribute value // BBB [position () = floor (last () div 2 + 0.5) or position () = ceiling (last () div 2 + 0.5)]
Case 2: Use DOM4J + Xpath for user login and Registration
Code implementation:
1. Compile an information file users_login.xml for user login. <? Xml version = "1.0" encoding = "UTF-8"?> <Users> <user name = "jack" password = "root"/> </users>
2. login business method // provides a method for login public static User login (String name, String password, File file) throws Exception {Document doc = getDoc (file ); node node = doc. selectSingleNode ("/users/user [@ name = '" + name + "' and @ password = '" + password + "']"); User user = null; if (node! = Null) {String name_value = node. valueOf ("@ name"); String password_value = node. valueOf ("@ password"); user = new User (name_value, password_value);} return user ;}
3. the registered business logic method // provides a business logic to store a User public static void registUser (User user, File) throws Exception {Document doc = getDoc (file ); element root = doc. getRootElement (); Element new_user = incluenthelper. createElement ("user "). addAttribute ("name", user. getName ()). addAttribute ("password", user. getPassword (); root. add (new_user); // persists the document Object to the file XMLWriter writer = new XMLWriter (new OutputStreamWriter (new FileOutputStream (file), "UTF-8"); writer. write (doc); writer. close ();}
Comparison: dom vs sax vs DOM4J + XPATH
DOM loads XML files into memory for parsing in the form of a DOM tree. Node, Element, Document, Text, Attr, etc.
Disadvantage: XML files of big data cannot be parsed.
Priority: You can perform any operations on XML.
SAX divides XML parsing into different time periods. Therefore, you need to create your own event processor class.
StartDocument ()/endDocument startElement ()/endElement () characater ()
Disadvantage: only elements can be obtained.
Priority: fast event mechanism.
DOM4J + XPATH
DOM4J + XPATH combines the respective advantages of DOM and SAX and discards the disadvantages of both.
The following is a summary: DOM4J + XPATH is preferred.
Problem: 1. Use DOM to store a node, but the same root element in the xml file disappears. The DOM tree is not written back to the XML file. New DomSource (dom) 2. Insert an element before the specified position. When calling the insertBefore () method, you should call this method of the direct parent class.
Summarize and master the principles and ideas for parsing XML. DocumentBuilderFactory/DocumentBuilder TtransformerFactory/TtransformerSaxPaserFactory/SaxParserSaxReader/XMLWriter/DocuementHelper/Docuement
Web Server Introduction
A Web Server is a computer installed with web server software. Web server software is a software that provides external access to the resources of folders managed by itself.
The main task of Web server software is to obtain the user's requested resources. You can search for resources on your managed Website Based on the Resource Name and use the input stream to read the resource data, and send it to the client browser as an output stream. Socket programming is used at the underlying layer of Web server software.
WebServer Software |
Vendor |
IIS |
Microsoft |
Tomcat |
Apache open-source server |
Web Logic |
BEA |
Web Sphere |
IBM |
JBoss |
RedHat |
1. Web Concept
Web is a Web page. Therefore, to develop a website, you must start developing a webpage. HTML + CSS + JavaScript
2 static and dynamic concepts
Static webpage: the source code of each page view is a static webpage if it is unchangeable.
Dynamic Web page: the source code of each page is a static Web page if it is variable.
HTML + CSS + JavaScript + Flash à static
JSP and Servlet technologies are dynamic
3 concept of software architecture
Software architecture is a way to deploy software after software.
CS structure: Client/Server, for example, QQ
Download and install the client program. In addition, data must be sent to the server.
Disadvantage: Upgrade
Bsstructure Browser/Server, for example, Web QQ
No additional download client is required. You only need a browser.
Advantage: no installation and upgrade required
Websites belong to the bsstructure. Thin client program.
Tomcat server
Tomcat is developed by the apache open-source organization using the java language. Therefore, JRE is used during runtime. However, developers can directly configure JDK.
1. Download
Http://www.apache.org Tomcat
2. Installation
Directly decompress (green version)
3. Configuration
Java_home = JDK root directory
4. Test
Go to the installed bin directory.
Open the browser and enter http: // 127.0.0.1: 8080/visible.