<!--
xml escape characters: characters with special meanings can be expressed using escape characters
< <
> >
" "
' '
& &
Requirement: Writing of 1 < 3 == 5 > 7 in xml
-->
.<!--
Demand:
You can use escape characters to display the following in text
<bean id="abc">
<property name="haha" value="123"></property>
<property name="haha" value="123"></property>
<property name="haha" value="123"></property>
</bean>
You can use the CDATA area to do this.
CDATA area: The content inside will be displayed in text.
Format:
<![CDATA[
Arbitrary content (all text)
]]>
-->
<!--
Steps to constrain documents using dtd:
1. Each dtd constraint document will have a line of code that starts with <! DOCTYPE and copies it into an xml document.
a.!DOCTYPE: Fixed dtd constraint document format
b.beans: Provides that the root element in an xml document can only be called beans
c.SYSTEM: System, dtd constraints document source local operating system
d."bean.dtd": The location of the constraint document. The constraint document we use is in the current folder, you can use "bean.dtd"
2. Write the root element according to its name.
3. Put the mouse on the root element / or press f2 to write the xml document according to the prompt
?: Representation elements can only occur 0 or 1 times
+ Representation elements can occur one or more times
* Representation elements can occur 0 times, 1 time or more (any number of times)
(): A set of data
| Choose relationships, only one among multiple elements
The order relation stipulates that xml documents written in a, B and C can only be written in a, B and C order.
-->
<!--
1. Each schema document will have a start tag < beans...> for the root element, copy the start tag and add an end tag.
2. Put the mouse on the root element / f2 and write the xml document according to the prompt
-->
<!--
Use of schema-constrained documents
1. Every schema constraint document must have a namespace.
Naming requirements: the only one in the world
Name using domain name (address): http://www.itcast.cn/web01/01
Namespaces in bean-schema.xsd documents
targetNamespace="http://www.itcast.cn/bean"
2. Declare namespaces in xml documents
Default declaration:
xmlns="http://www.itcast.cn/bean"
<bean></bean>
Display declaration:
xmlns:my="http://www.itcast.cn/bean"
<my:bean></my:bean>
3. Declare the location of schema constraint documents
First declare the location of the official document
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Declare the location of the custom schema document according to the official document
xsi:schemaLocation="http://www.itcast.cn/bean bean-schema.xsd"
http://www.itcast.cn/bean: Namespaces
Bean-schema.xsd: Location of schema documents
-->
Parsing xml documents using dom4j
* 1.SAXReader object (dom4j core class)
a) read(... ) load and execute xml documents
2. Document object
a) Element e = getRootElement() Gets the root element
3. Element objects
Element [] eleArr = elements(... ) Gets all the child elements of the specified name. You may not specify a name
b) element(... ) Gets the first child element of the specified name. You may not specify a name
c) getName() gets the element name of the current element
d) attributeValue(... ) Gets the property value of the specified property name
e) elementText(... ) Gets the text value of the specified name child element
f) getText() Gets the text content of the current element
Operation steps:
1. Create the dom4j core object SAXReader
2. Use read method in SAXReader to read xml document and generate Document object
3. Use the method getRootElement in Document to get the root element Element
4. Get all bean elements using the method elements in Element
5. Traverse the collection containing bean elements to get each bean element
6. Use the method attributeValue in Element to get the value of the attribute on the bean element
7. Get all property elements using the method elements in Element
8. Traverse through a collection of property elements to get each property element
9. Use the method attributeValue in Element to get the value of the attribute on the property element
10. Get the text value above the property element using the method getText in Element
1 public class UseDom4jParseXML { 2 public static void main(String[] args) throws Exception { 3 //1.Establish dom4j Core Objects SAXReader 4 SAXReader sax = new SAXReader(); 5 //2.Use SAXReader in read Method Reading xml File,generate Document object 6 Document docu = sax.read("bean.xml"); 7 //3.Use Document Chinese method getRootElement Get the root element Element 8 Element rootElement = docu.getRootElement(); 9 //4.Use Element Method in elements Get all bean element 10 List<Element> beanElementList = rootElement.elements(); 11 //5.Ergodic inclusion bean Collection of elements,Get each bean element 12 for (Element beanElement : beanElementList) { 13 String beanName = beanElement.getName(); 14 System.out.println(beanName); 15 //6.Use Element Method in attributeValue Obtain bean Values of attributes on elements 16 String idValue = beanElement.attributeValue("id"); 17 System.out.println("\tbean Attributes of elements id:"+idValue); 18 String classNameValue = beanElement.attributeValue("className"); 19 System.out.println("\tbean Attributes of elements className:"+classNameValue); 20 //7.Use Element Method in elements Get all property element 21 List<Element> propertyElementList = beanElement.elements(); 22 //8.Ergodic inclusion property Collection of elements,Get each property element 23 for (Element propertyElement : propertyElementList) { 24 System.out.println("\t\t"+propertyElement.getName()); 25 //9.Use Element Method in attributeValue Obtain property Values of attributes on elements 26 String nameValue = propertyElement.attributeValue("name"); 27 System.out.println("\t\t\tproperty Attributes of elements name:"+nameValue); 28 String valueValue = propertyElement.attributeValue("value"); 29 System.out.println("\t\t\tproperty Attributes of elements value:"+valueValue); 30 //10.Use Element Method in getText Acquisition property Text values on elements 31 String text = propertyElement.getText(); 32 System.out.println("\t\t\tproperty Element text:"+text); 33 } 34 } 35 } 36 }
BeanUtils Tool Class
Use BeanUitls Common to inject (assign) values to member variables in a class
1 public class Demo01BeanUtils { 2 @Test 3 public void demo03() throws Exception{ 4 /* 5 * BeanUitls The method populate in the 6 * populate(Object bean, Map<String,String[]> properties) 7 * Encapsulating Map data into a specified Java bean is generally used to encapsulate all the data of a form into a JavaBean 8 * Equivalent to traversing the Map set, according to the key (attribute name) of the Map set, the value of the Map set is used in turn to inject values into the member variables. 9 * 10 * Parameters: 11 * Object bean:JavaBean objects to be assigned 12 * Map<String,String[]> properties:Map aggregate 13 * key:String,Name of member variable (attribute name) 14 * value:String[],Array of string type 15 * If the member variable is an array, then all values of the value array are assigned to the member variable. 16 * If the member variable is not an array, then the first value of the value array is used to assign the member variable. 17 * How arrays are created: 18 * Dynamic initialization: 19 * String[] arr = new String[The length of the array]; 20 * Static initialization: 21 * String[] arr = new String[]{Element value 1, element value 2,...}; 22 * String[] arr = {Element value 1, element value 2,...}; the definition and assignment of an array must be written in one line 23 */ 24 //Establish JavaBean object 25 User u = new User(); 26 //Establish Map aggregate,key yes String type,value yes String Array of types 27 Map<String,String[]> properties = new HashMap<String,String[]>(); 28 properties.put("id", new String[]{"123","456"}); 29 properties.put("username", new String[]{"root","admin"}); 30 properties.put("password", new String[]{"root","123456"}); 31 properties.put("hobbies", new String[]{"eat","sleep","play","Knock code"}); 32 //Use BeanUtils Medium populate Injecting multiple values into multiple member variables 33 BeanUtils.populate(u, properties); 34 35 System.out.println(u); 36 } 37 38 @Test 39 public void demo02() throws Exception{ 40 /* 41 * static void setProperty(Object obj,String name,Object value) Setting property values 42 * It's equivalent to calling the setXXX method in JavaBean 43 * static String getProperty(Object obj,String name) Get attribute values 44 * Equivalent to calling the getXXX method in JavaBean 45 * The parameters of the method are as follows: 46 * Object obj:JavaBean objects to assign / get values 47 * String name:Name of member variable (attribute name) 48 * Object value:Values assigned to member variables for actual use 49 * 50 * BeanUtils The data types that tool classes can operate on are: basic data types, packaging classes of basic data types, String types, and one-dimensional arrays of the above three types. 51 * Regardless of the data type of the attribute, the BeanUtils tool class can use the numeric type of the character format to assign values (internally converting strings to basic types is equivalent to using 52 * The method parseXXX()) in the wrapper class 53 */ 54 //Establish JavaBean object 55 User u = new User(); 56 //Use BeanUtils Medium setProperty Method injects values into member variables 57 BeanUtils.setProperty(u, "id", "123"); 58 BeanUtils.setProperty(u, "username", "root"); 59 BeanUtils.setProperty(u, "password", "root"); 60 System.out.println(u); 61 62 //Use BeanUtils Medium getProperty Method to get attribute values 63 String id = BeanUtils.getProperty(u, "id"); 64 System.out.println(id); 65 String username = BeanUtils.getProperty(u, "username"); 66 System.out.println(username); 67 System.out.println(BeanUtils.getProperty(u, "password")); 68 } 69 70 @Test 71 public void demo01(){ 72 //Do not use tool classes,Assignment of member variables/Get the value 73 User u = new User(); 74 u.setId(Integer.parseInt("123")); 75 u.setUsername("root"); 76 u.setPassword("root"); 77 78 System.out.println(u.getId()); 79 System.out.println(u.getUsername()); 80 System.out.println(u.getPassword()); 81 } 82 }
1 /* 2 * Create MyBeanUtils Tool Classes to Enhance the populate Method 3 */ 4 public class MyBeanUtils { 5 //Private the construction method,Do not let the outside world call methods by creating objects 6 private MyBeanUtils() { 7 } 8 9 /* 10 * Define a method (allowing users to use their own populate method without handling exceptions) 11 * 1.Parametric passing JavaBean object's Class file object 12 * 2.Creating Java bean objects internally through reflection 13 * 3.Calling the method populate of BeanUtils tool class 14 * 4.Attempt... catch handling of the exception of populate method 15 * 5.Return the object to the user 16 * 6.Add a generic type to the parameter Class object to let the user pass any type of JavaBean and return any type of JavaBean. 17 * 18 * Define methods with generics: Determine data types when calling methods 19 * Modifier < define generics > return value type method name (parameter < use generics >){ 20 * Method body 21 * } 22 * 23 * 24 * The parameters of the method are as follows: 25 * Class clazz 26 * Map<String,String[]> properties 27 * The return value type of the method: 28 * Object 29 */ 30 public static <E> E populate03(Class<E> clazz, Map<String,String[]> properties){ 31 try { 32 //2.Internally created by reflection Javabean object 33 E obj = clazz.newInstance(); 34 //3.call BeanUtils Method of Tool Class populate 35 BeanUtils.populate(obj, properties); 36 //5.Return the object to the user 37 return obj; 38 } catch (Exception e) { 39 //4.Yes populate Abnormal progress of method try...catch Handle 40 e.printStackTrace(); 41 //Compile exceptions,Converting to runtime exceptions,Failure to inject values into member variables,Stop the program 42 throw new RuntimeException("Injection Value Failure"); 43 } 44 } 45 46 /* 47 * Define a method (allowing users to use their own populate method without handling exceptions) 48 * 1.Parametric passing JavaBean object's Class file object 49 * 2.Creating Java bean objects internally through reflection 50 * 3.Calling the method populate of BeanUtils tool class 51 * 4.Attempt... catch handling of the exception of populate method 52 * 5.Return the object to the user 53 * 54 * The parameters of the method are as follows: 55 * Class clazz 56 * Map<String,String[]> properties 57 * The return value type of the method: 58 * Object 59 */ 60 public static Object populate02(Class clazz, Map<String,String[]> properties){ 61 try { 62 //2.Internally created by reflection Javabean object 63 Object obj = clazz.newInstance(); 64 //3.call BeanUtils Method of Tool Class populate 65 BeanUtils.populate(obj, properties); 66 //5.Return the object to the user 67 return obj; 68 } catch (Exception e) { 69 //4.Yes populate Abnormal progress of method try...catch Handle 70 e.printStackTrace(); 71 //Compile exceptions,Converting to runtime exceptions,Failure to inject values into member variables,Stop the program 72 throw new RuntimeException("Injection Value Failure"); 73 } 74 75 } 76 77 /* 78 * Define a method (allowing users to use their own populate method without handling exceptions) 79 * 1.Calling the method populate of BeanUtils tool class 80 * 2.Attempt... catch handling of the exception of populate method 81 * 82 * The parameters of the method are as follows: 83 * Object bean 84 * Map<String,String[]> properties 85 * The return value type of the method: 86 * void 87 */ 88 public static void populate01(Object bean, Map<String,String[]> properties){ 89 try { 90 //1.call BeanUtils Method of Tool Class populate 91 BeanUtils.populate(bean, properties); 92 } catch (Exception e) { 93 //2.Yes populate Abnormal progress of method try...catch Handle 94 e.printStackTrace(); 95 //Compile exceptions,Converting to runtime exceptions,Failure to inject values into member variables,Stop the program 96 throw new RuntimeException("Injection Value Failure"); 97 } 98 } 99 } 100 package cn.itcast.dmeo03.MyBeanUtils; 101 102 import java.util.HashMap; 103 import java.util.Map; 104 105 import org.junit.Test; 106 107 import cn.itcast.dmeo01.bean.User; 108 109 /* 110 * Use the custom tool class MyBeanUtils 111 */ 112 public class UseMyBeanUtils { 113 @Test 114 public void demo03(){ 115 //Establish Map aggregate,key yes String type,value yes String Array of types 116 Map<String,String[]> properties = new HashMap<String,String[]>(); 117 properties.put("id", new String[]{"123"}); 118 properties.put("username", new String[]{"root","admin"}); 119 properties.put("password", new String[]{"root","123456"}); 120 properties.put("hobbies", new String[]{"eat","sleep","play","Knock code"}); 121 //call MyBeanUtils Methods in Tool Classes populate02 122 User u = MyBeanUtils.populate03(User.class, properties); 123 System.out.println(u); 124 } 125 126 @Test 127 public void demo02(){ 128 //Establish Map aggregate,key yes String type,value yes String Array of types 129 Map<String,String[]> properties = new HashMap<String,String[]>(); 130 properties.put("id", new String[]{"123"}); 131 properties.put("username", new String[]{"root","admin"}); 132 properties.put("password", new String[]{"root","123456"}); 133 properties.put("hobbies", new String[]{"eat","sleep","play","Knock code"}); 134 //call MyBeanUtils Methods in Tool Classes populate02 135 User u = (User) MyBeanUtils.populate02(User.class, properties); 136 System.out.println(u); 137 } 138 139 @Test 140 public void demo01(){ 141 //Establish JavaBean object 142 User u = new User(); 143 //Establish Map aggregate,key yes String type,value yes String Array of types 144 Map<String,String[]> properties = new HashMap<String,String[]>(); 145 properties.put("id", new String[]{"123"}); 146 properties.put("username", new String[]{"root","admin"}); 147 properties.put("password", new String[]{"root","123456"}); 148 properties.put("hobbies", new String[]{"eat","sleep","play","Knock code"}); 149 //call MyBeanUtils Methods in Tool Classes populate01 150 MyBeanUtils.populate01(u, properties); 151 System.out.println(u); 152 } 153 }
1 /* 2 * Comprehensive case: XML+dom4j + reflection + BeanUtils 3 * 1.Storing the class name and attribute name attribute values of JavaBean s using xml 4 * 2.Parsing xml using dom4j 5 * 3.Creating JavaBean Objects by Resolving Full Class Names Using Reflection Technology 6 * 4.Use the method setProperty in BeanUtils to inject values into member variables 7 */ 8 public class UseDom4jparseXMLToJavaBean { 9 public static void main(String[] args) throws Exception { 10 //2.Use dom4j analysis xml 11 //Obtain dom4j Core Classes SAXReader 12 SAXReader sax = new SAXReader(); 13 //Use SAXReader Medium read read xml,Establish Document object 14 Document docu = sax.read("src/cn/itcast/dmeo05/domain/data.xml"); 15 //Use Document Method in getRootElement Get the root element 16 Element rootElement = docu.getRootElement(); 17 //Use Element Method in elements Get all bean element,Put it into a collection 18 List<Element> beanElementList = rootElement.elements(); 19 //ergodic beanElementList aggregate 20 for (Element beanElement : beanElementList) { 21 //Obtain beanElement Variable attributes on className 22 String className = beanElement.attributeValue("className"); 23 //3.Create a class name based on the parsed full class name using reflection technology JavaBean object 24 Class clazz = Class.forName(className); 25 Object obj = clazz.newInstance(); 26 //Use Element Method in elements Get all property element,Put it into a collection 27 List<Element> propertyElementList = beanElement.elements(); 28 //ergodic propertyElementList aggregate 29 for (Element propertyElement : propertyElementList) { 30 //Obtain propertyElement Attributes on name(Property name)and value(Attribute value) 31 String name = propertyElement.attributeValue("name"); 32 String value = propertyElement.attributeValue("value"); 33 //4.Use BeanUtils Method in setProperty Inject values into member variables 34 BeanUtils.setProperty(obj, name, value); 35 } 36 //Printing JavaBean object 37 System.out.println(obj); 38 } 39 } 40 41 }