XML file
xml file is an extensible markup language, which is used to store and transmit data;
HTML as hypertext markup language; It is used to display data
The disadvantage is that the syntax of xml is cumbersome. In the early days, we still had to parse xml files and read and write data in Java language
XML can manage the data written in various programming languages, so that XML data can be read by parser on any platform.
Naming rules for XML
(1) There is no character limit for names. Alphanumeric characters can be used (there is no reserved word), but they cannot start with numbers or punctuation, or with the character "XML" (or XML, Xml)
(2) The write name cannot contain spaces;
Let's look at Tomcat's web.xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> </web-app>
XML is mainly divided into four parts,
- Document declaration
Declare the optional part of the file, if any, on the first line of the document
<?xml version="1.0" encoding="UTF-8"?>
- Element (label)
Note that there can only be one root element; other elements can be nested in the element, or empty elements can appear (that is, only labels are written, and no content is written inside)
It should be noted that the written label must be closed;
Labels are case sensitive
For example, when I customize the xml file, I start to write a root element users, but write another root element below, and an error prompt multiple root tags appears
- attribute
Attributes are used to decorate elements
If you want to write more attributes on an element, be careful that the attribute names are not the same
When writing attribute values, remember not to write & or 'or <; and attribute values must be enclosed in double or single quotation marks;
For example, if I write two attribute word s with the same attribute name in the same tag name, an error occurs
Prompt duplicate attribute word
- notes
Notes use <! --- >; write your own explanation of what needs to be explained
Syntax rules for XML
First of all, it should be noted that do not leave spaces at will. Spaces will be retained;
In XML, there are five predefined entity references:
quote | express | meaning |
---|---|---|
< | < | Less than |
> | > | Greater than |
& | & | And (ampersand) |
' | ' | Single quotation mark (apostrophe) |
" | " | Quotation mark |
XML constraints
You can write a document constraint to constrain the writing specification of xml,
- DTD constraints: the syntax is relatively simple, the function is simple, and the learning cost is low.
- Schema constraint: the syntax is relatively complex and powerful. The learning cost is high
Tomcat's xml constraint is the Schema constraint;
reflex
When you need to get the object of a class before, you need to design and define a class first, and then create the object of the class by using the new + construction method; then you can call the properties or methods of the class through the object
But this approach is not very flexible
reflex
The reflection mechanism only needs the class name to dynamically obtain the class information and the class object
In fact, I have been exposed to reflection before learning;
For example, when configuring a servlet in the web.xml configuration file, the defined servlet class information is obtained through the class name
In fact, it can be understood as: first parse the xml file, then read the class address, and obtain the class object through the reflection mechanism according to the class address
The second case is that when learning Jdbc, the mechanism of loading classes is reflection
Class.forName("com.mysql.cj.jdbc.Driver");
Reflective understanding
Whenever a class is used, the class loader reads the bytecode information of the class into memory, and creates a class object for each class. The class information can be obtained through the class object
The entity class Person used in the exercise
public class Person { public String name; private int age; private int id; public Person(){ System.out.println("Nonparametric construction method"); } public Person(String name,int id,int age){ this.name=name; this.age=age; this.id=id; System.out.println("Parametric construction method-->name-->id-->age"); } private Person(int age){ System.out.println("Private construction method-->id"); } public void run(){ System.out.println("run method"); } public void run(String name){ System.out.println("run method-->name"); } private void run(String name,int age){ System.out.println("Private modification run method-->name-->age"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
Three ways to get Class objects
(1) class name. class
//Method 1: directly use the class name. Class; Class person1 = Person.class; System.out.println(person1);
return
class com.xiaozhi.pojo.Person
(2) Use the getClass() method of the Object class
//Method 2: first get the object of Person class, and then use getClass to get it; Person p=new Person(); Class person2 = p.getClass(); System.out.println(person2);
return
Nonparametric construction method class com.xiaozhi.pojo.Person
(3) Through the forName(String name) method of Class
//Method 3: pass the forName method of Class; the premise is to know the path address of Class; //According to the address of the Person Class, load the Class information into the memory, and then obtain the Class object; the Class information can be operated dynamically; Class person3 = Class.forName("com.xiaozhi.pojo.Person"); System.out.println(person3);
return
class com.xiaozhi.pojo.Person
In fact, the results obtained by these methods are the same
Gets the constructor information of the class
😃 (1) The getConstructor() method of class can obtain the public parameterless constructor method
public Constructor getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
The newInstance() method of the Constructor class can also create objects for the class;
public Object newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, reflect.InvocationTargetException
practice
//Pass the forName method of Class; the premise is to know the path address of Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //getConstructor() method of Class; get the public parameterless constructor method; Constructor constructor = person.getConstructor(); System.out.println(constructor); //newInstance of Constructor class can create objects for the class; Person p=(Person)constructor.newInstance(); System.out.println(p);
Output:
public com.xiaozhi.pojo.Person() Nonparametric construction method com.xiaozhi.pojo.Person@74a14482
😃 (2) Getconstructor (class <? >... Parametertypes) method of class; parameter constructor method for obtaining public specified parameter list;
public Constructor getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
practice
//Pass the forName method of Class; the premise is to know the path address of Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //Getconstructor (Class <? >... Parametertypes) method of Class; parameter constructor method for obtaining public specified parameter list; Constructor constructor = person.getConstructor(String.class,int.class,int.class); System.out.println(constructor); //Create objects according to the obtained construction method; Person p=(Person)constructor.newInstance("Xiao Zhi",21,178); System.out.println(p);
Output:
public com.xiaozhi.pojo.Person(java.lang.String,int,int) Parametric construction method-->name-->id-->age com.xiaozhi.pojo.Person@74a14482
😃 (3) getConstructors() method of class; get all public constructors;
public Constructor<?>[] getConstructors() throws SecurityException
practice:
//getConstructors() method of Class; get all public constructors; Constructor[] constructors = person.getConstructors(); for (Constructor constructor : constructors) { System.out.println(constructor); }
Output:
public com.xiaozhi.pojo.Person(java.lang.String,int,int) public com.xiaozhi.pojo.Person()
😃 (4) Getdeclaraedconstructor (class <? >... Parametertypes) method of class to obtain the constructor of the specified parameter list, including the constructor of private decoration
public Constructor getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
practice
//Pass the forName method of Class; the premise is to know the path address of Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //Getdeclaraedconstructor (Class <? >... Parametertypes) of Class gets the construction method of the specified parameter list; Constructor declaredConstructor = person.getDeclaredConstructor(String.class,int.class,int.class); System.out.println(declaredConstructor); Constructor declaredConstructor1 = person.getDeclaredConstructor(int.class); System.out.println(declaredConstructor1);
output
public com.xiaozhi.pojo.Person(java.lang.String,int,int) private com.xiaozhi.pojo.Person(int)
However, when you get the construction method of private modification, you need to solve the permission problem;
//Pass the forName method of Class; the premise is to know the path address of Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //When it comes to methods of private modification; Constructor declaredConstructor1 = person.getDeclaredConstructor(int.class); Person p= (Person) declaredConstructor1.newInstance(21); System.out.println(p);
An exception occurs; the prompt IllegalAccessException does not have access permission
Exception in thread "main" java.lang.IllegalAccessException: Class com.xiaozhi.pojo.Test2 can not access a member of class com.xiaozhi.pojo.Person with modifiers "private"
Then set the manual opening permission;
The Constructor class has such a parent class AccessibleObject
There is such a method setAccessible(boolean flag) to set whether to check access permissions; the default is false
A value of true indicates that the reflected object should suppress Java language access checks when used. A value of false indicates that the reflected object should enforce Java language access checks.
Then try it
//When it comes to methods of private modification; Constructor declaredConstructor1 = person.getDeclaredConstructor(int.class); //Refuse permission check; declaredConstructor1.setAccessible(true); Person p= (Person) declaredConstructor1.newInstance(21); System.out.println(p);
output
Private construction method-->id com.xiaozhi.pojo.Person@74a14482
😃 (5) Getdeclaraedconstructors () of class gets all construction methods
public Constructor<?>[] getDeclaredConstructors() throws SecurityException
practice
//Pass the forName method of Class; the premise is to know the path address of Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //Getdeclaraedconstructors () of Class gets all construction methods Constructor[] declaredConstructors = person.getDeclaredConstructors(); for (Constructor declaredConstructor : declaredConstructors) { System.out.println(declaredConstructor); }
output
private com.xiaozhi.pojo.Person(int) public com.xiaozhi.pojo.Person(java.lang.String,int,int) public com.xiaozhi.pojo.Person()
Get the properties and methods of the class
(1) getFields() method of class; get all public attributes;
public Field[] getFields() throws SecurityException
getName() method of file class; Get the attribute name;
public String getName()
Get (object) method of file class; Get attribute value;
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
practice
//Through the forName method of Class; The premise is to know the path address of the Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //First, create an object with the construction method; Constructor declaredConstructor = person.getConstructor(); Person p= (Person) declaredConstructor.newInstance(); //getFields() method of Class gets all public attributes; Field[] fields = person.getFields(); for (Field field : fields) { //getName() method of file class; Get the attribute name; System.out.println("Person Properties of class=>"+field.getName()); //Get (object) method of file class; Get attribute value; System.out.println("Person Property value of class=>"+field.get(p)); }
Output;
Nonparametric construction method Person Properties of class=>name Person Property value of class=>null
(2) Getdeclaraedfields() method of class; Get all attributes; Include private decorated properties
public Field[] getDeclaredFields() throws SecurityException
practice
//Through the forName method of Class; The premise is to know the path address of the Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //First, create an object with the construction method; Constructor declaredConstructor = person.getDeclaredConstructor(String.class,int.class,int.class); Person p= (Person) declaredConstructor.newInstance("Xiao Zhi",21,178); //Getdeclaraedfields() method of Class gets all attributes; Field[] fields = person.getDeclaredFields(); for (Field field : fields) { //Opening permission is required; field.setAccessible(true); //getName() method of file class; Get the attribute name; System.out.println("Person Properties of class=>"+field.getName()); //Get (object) method of file class; Get attribute value; System.out.println("Person Property value of class=>"+field.get(p)); }
output
Parametric construction method-->name-->id-->age Person Properties of class=>name Person Property value of class=>Xiao Zhi Person Properties of class=>age Person Property value of class=>21 Person Properties of class=>id Person Property value of class=>178
(3) Getdeclaraedmethod () method of class gets the specified name and specifies the method of parameter list
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
practice
//Through the forName method of Class; The premise is to know the path address of the Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); // Getdeclaraedmethod method of Class gets the specified name and specifies the method of parameter list Method run = person.getDeclaredMethod("run",String.class,int.class); System.out.println(run);
output
private void com.xiaozhi.pojo.Person.run(java.lang.String,int)
(4) getDeclaredMethods() method of class to obtain all methods;
public Method[] getDeclaredMethods() throws SecurityException
practice
//Through the forName method of Class; The premise is to know the path address of the Class; Class person = Class.forName("com.xiaozhi.pojo.Person"); //getDeclaredMethods() method of Class to obtain all methods; Method[] methods = person.getDeclaredMethods(); for (Method method : methods) { System.out.println(method);
output
private void com.xiaozhi.pojo.Person.run(java.lang.String,int) public void com.xiaozhi.pojo.Person.run(java.lang.String) public void com.xiaozhi.pojo.Person.run() public java.lang.String com.xiaozhi.pojo.Person.getName() public int com.xiaozhi.pojo.Person.getId() public void com.xiaozhi.pojo.Person.setName(java.lang.String) public void com.xiaozhi.pojo.Person.setId(int) public int com.xiaozhi.pojo.Person.getAge() public void com.xiaozhi.pojo.Person.setAge(int)