Xml parsing and case simulation of user logon using xpath

Source: Internet
Author: User
Problem: It is troublesome to use dom4j to query nodes (tags, attributes, and text) with deep hierarchies !!! Problem: It is troublesome to use dom4j to query nodes (tags, attributes, and text) with deep hierarchies !!!

Xpath is generated in this case-mainly used to quickly obtain the desired [node object].


How to use xPath technology in dom4j


1) importing xPath supports jar packages. Jaxen-1.1-beta-6.jar

2) use the xpath method

List SelectNodes ("xpath expression"); queries multiple node objects

Node selectSingleNode ("xpath expression"); queries a Node object


XPath syntax


/Absolute path indicates starting from the root position of xml or sub-elements (a hierarchy)

// Relative path indicates the selection element regardless of any hierarchy.

* Wildcard indicates that all elements are matched.

[] Condition indicates the element under which to select

@ Attribute indicates selecting an attribute node

And relationship indicates the relationship between conditions (equivalent &&)

Text () indicates select text content



Case


User logon:

Enter the user name and password-> go to "database" to check whether there is a corresponding user->

Yes: the logon is successful.

No: the logon fails.

Use xml as a database

User. xml is used to store user data.

The code is as follows:


Import java. io. bufferedReader; import java. io. file; import java. io. inputStreamReader; import org. dom4j. document; import org. dom4j. element; import org. dom4j. io. SAXReader;/*** xpath case: simulate user logon effect * @ author APPle **/public class Demo3 {public static void main (String [] args) throws Exception {// 1. obtain the user name and password entered by the user. BufferedReader br = new BufferedReader (new InputStreamReader (System. in); // encapsulate keyboard input and input the stream System. out. print Ln ("Enter the user name:"); String name = br. readLine (); System. out. println ("enter the password:"); String password = br. readLine (); // 2. query "database" to see if there is a corresponding user // corresponding user: in the user. in the xml file, find the user tag Document doc = new SAXReader () whose/name attribute value is 'user input' and whose password attribute value is 'user input (). read (new File (". /src/user. xml "); Element userElem = (Element) doc. selectSingleNode ("// user [@ name = '" + name + "' and @ password = '" + password + "']"); // method for concatenating variables in a string-first add a double quotation mark and move the cursor to the middle of the double quotation mark , Write two plus signs, and move the cursor to the center of the plus sign to write the variable. // System. out. println (userElem. getName (); // view the content of the current node object if (userElem! = Null) {// The user name and password are found in the database. // System. out. println ("logon successful");} else {// logon failed System. out. println ("logon failed ");}}}



The above content is the xml parsing of xpath technology and the simulated user login effect in the case. For more information, see PHP Chinese network (www.php1.cn )!

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.