zoukankan      html  css  js  c++  java
  • 基础加强_XML

    <!--
    xml转义字符:可以把一些具有特殊含义的字符,使用转义字符表示出来
    < &lt;
    > &gt;
    " &quot;
    ' &apos;
    & &amp;
    需求:1<3 == 5>7 在xml中的写法
    -->

    .<!--
    需求:
    把以下内容以文本的形式显示出来,可以使用转义字符
    <bean id="abc">
    <property name="haha" value="123"></property>
    <property name="haha" value="123"></property>
    <property name="haha" value="123"></property>
    </bean>
    可以使用CDATA区完成
    CDATA区:里边的内容都会以文本的方式展示出来
    格式:
    <![CDATA[
    任意的内容(都是文本)
    ]]>
    -->

    <!--
    使用dtd约束文档的步骤:
    1.每一个dtd约束文档中都会有一行代码以<!DOCTYPE开头,复制过来放在xml文档中
    a.!DOCTYPE:固定的dtd约束文档的格式
    b.beans:规定xml文档中的根元素只能叫beans
    c.SYSTEM:系统,dtd约束文档来源本地的操作系统
    d."bean.dtd":约束文档的位置,我们使用的约束文档在当前文件夹下,可以使用"bean.dtd"
    2.根据根元素的名字,写出根元素
    3.鼠标放在根元素上/或者按f2,根据提示写出xml文档
    ?:代表元素只能出现0次或者1次
    +:代表元素能出现1次或者1次以上
    *:代表元素能出现0次,1次或者1次以上(任意次数)
    ():一组数据
    |:选择关系,只能在多个元素之间选择一个
    ,顺序关系 规定:a,b,c 写xml文档只能按照a,b,c顺序编写
    -->

    <!--
    1.每一个schema文档都会有一个根元素的开始标签<beans...>,把开始标签复制过来,添加一个结束标签
    2.鼠标放在根元素上/f2,根据提示写出xml文档
    -->

    <!--
    schema约束文档的使用
    1.每一个schema约束文档,都必须有一个命名空间(namespace)
    起名要求:全球唯一
    使用域名(网址)命名:http://www.itcast.cn/web01/01
    bean-schema.xsd文档的中命名空间
    targetNamespace="http://www.itcast.cn/bean"
    2.在xml文档中声明命名空间
    默认声明:
    xmlns="http://www.itcast.cn/bean"
    <bean></bean>
    显示声明:
    xmlns:my="http://www.itcast.cn/bean"
    <my:bean></my:bean>
    3.声明schema约束文档的位置
    先声明官方文档的位置
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    根据官方文档声明自定义schema文档的位置
    xsi:schemaLocation="http://www.itcast.cn/bean bean-schema.xsd"
    http://www.itcast.cn/bean:命名空间
    bean-schema.xsd:schema文档的位置
    -->

    使用dom4j解析xml文档
    * 1.SAXReader对象(dom4j核心类)
    a) read(…) 加载执行xml文档
    2. Document对象
    a) Element e = getRootElement() 获得根元素
    3. Element对象
    a) Element [] eleArr = elements(…) 获得指定名称的所有子元素。可以不指定名称
    b) element(…) 获得指定名称第一个子元素。可以不指定名称
    c) getName() 获得当前元素的元素名
    d) attributeValue(…) 获得指定属性名的属性值
    e) elementText(…) 获得指定名称子元素的文本值
    f) getText() 获得当前元素的文本内容
    操作步骤:
    1.创建dom4j核心对象SAXReader
    2.使用SAXReader中read方法读取xml文档,生成Document对象
    3.使用Document中方法getRootElement获取根元素Element
    4.使用Element中的方法elements获取所有bean元素
    5.遍历包含bean元素的集合,获取每一个bean元素
    6.使用Element中的方法attributeValue获取bean元素上属性的值
    7.使用Element中的方法elements获取所有property元素
    8.遍历包含property元素的集合,获取每一个property元素
    9.使用Element中的方法attributeValue获取property元素上属性的值
    10.使用Element中的方法getText获取获取property元素上文本值

     1 public class UseDom4jParseXML {
     2     public static void main(String[] args) throws Exception {
     3         //1.创建dom4j核心对象SAXReader
     4         SAXReader sax = new SAXReader();
     5         //2.使用SAXReader中read方法读取xml文档,生成Document对象
     6         Document docu = sax.read("bean.xml");
     7         //3.使用Document中方法getRootElement获取根元素Element
     8         Element rootElement = docu.getRootElement();
     9         //4.使用Element中的方法elements获取所有bean元素
    10         List<Element> beanElementList = rootElement.elements();
    11         //5.遍历包含bean元素的集合,获取每一个bean元素
    12         for (Element beanElement : beanElementList) {
    13             String beanName = beanElement.getName();
    14             System.out.println(beanName);
    15             //6.使用Element中的方法attributeValue获取bean元素上属性的值
    16             String idValue = beanElement.attributeValue("id");
    17             System.out.println("	bean元素的属性id:"+idValue);
    18             String classNameValue = beanElement.attributeValue("className");
    19             System.out.println("	bean元素的属性className:"+classNameValue);
    20             //7.使用Element中的方法elements获取所有property元素
    21             List<Element> propertyElementList = beanElement.elements();
    22             //8.遍历包含property元素的集合,获取每一个property元素
    23             for (Element propertyElement : propertyElementList) {
    24                 System.out.println("		"+propertyElement.getName());
    25                 //9.使用Element中的方法attributeValue获取property元素上属性的值
    26                 String nameValue = propertyElement.attributeValue("name");
    27                 System.out.println("			property元素的属性name:"+nameValue);
    28                 String valueValue = propertyElement.attributeValue("value");
    29                 System.out.println("			property元素的属性value:"+valueValue);
    30                 //10.使用Element中的方法getText获取获取property元素上文本值
    31                 String text = propertyElement.getText();
    32                 System.out.println("			property元素的文本:"+text);
    33             }
    34         }
    35     }
    36 }

    BeanUtils工具类
    使用BeanUitls公共,给类中的成员变量注入(赋)值

     1 public class Demo01BeanUtils {
     2     @Test
     3     public void demo03() throws Exception{
     4         /*
     5          * BeanUitls中的方法populate给成员变量一起注入多个值
     6          * populate(Object bean, Map<String,String[]> properties)       
     7          *         将Map数据封装到指定Javabean中,一般用于将表单的所有数据封装到javabean
     8          *         相当于遍历Map集合,根据Map集合的key(属性名),依次使用Map集合value,给成员变量注入值
     9          * 
    10          *     参数:
    11          *         Object bean:要赋值的JavaBean对象
    12          *         Map<String,String[]> properties:Map集合
    13          *             key:String,成员变量的名字(属性名)
    14          *             value:String[],字符串类型的数组
    15          *                 如果成员变量是一个数组,那么就会把value数组的所有值赋值给成员变量
    16          *                 如果成员变量不是一个数组,那么就会使用value数组的第一个值,赋值给成员变量
    17          * 数组创建的方式:
    18          *     动态初始化:
    19          *         String[] arr = new String[数组的长度];
    20          *     静态初始化:
    21          *         String[] arr = new String[]{元素值1,元素值2,...};
    22          *         String[] arr = {元素值1,元素值2,...}; 数组的定义和赋值必须写在一行
    23          */
    24         //创建JavaBean对象
    25         User u = new User();
    26         //创建Map集合,key是String类型,value是String类型的数组
    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[]{"吃","睡","玩","敲代码"});
    32         //使用BeanUtils中的populate给多个成员变量注入多个值
    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)    设置属性值
    42          *         相当于调用了JavaBean中的setXXX方法
    43          * static String getProperty(Object obj,String name)    获得属性值
    44          *         相当于调用了JavaBean中的getXXX方法
    45          * 方法的参数:
    46          *         Object obj:要赋值/获取值的JavaBean对象
    47          *         String name:成员变量的名字(属性名)
    48          *         Object value:给成员变量赋的实际使用的值
    49          * 
    50          * BeanUtils工具类可以操作的数据类型有:基本数据类型,基本数据类型的包装类,String类型,以上3种类型的一维数组
    51          * 不管属性是什么数据类型,使用BeanUtils工具类都可以使用字符格式的数类型来赋值(内部会把字符串转换为基本类型,相当于使用了
    52          * 包装类中的方法parseXXX())
    53          */
    54         //创建JavaBean对象
    55         User u = new User();
    56         //使用BeanUtils中的setProperty方法给成员变量注入值
    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         //使用BeanUtils中的getProperty方法获取属性值
    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         //不使用工具类,给成员变量赋值/获取值
    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  * 创建MyBeanUtils工具类,增强populate方法
      3  */
      4 public class MyBeanUtils {
      5     //把构造方法私有,不让外界通过创建对象的方式调用方法
      6     private MyBeanUtils() {
      7     }
      8     
      9     /*
     10      * 定义一个方法(让用户使用自己定义的populate方法不用处理异常)
     11      *     1.参数传递JavaBean对象的Class文件对象
     12      *     2.内部通过反射创建Javabean对象
     13      *     3.调用BeanUtils工具类的方法populate
     14      *     4.对populate方法的异常进行try...catch处理
     15      *     5.把对象返回给用户
     16      *     6.把参数Class对象增加一个泛型,让用户传递什么类型的JavaBean,就返回什么类型的JavaBean
     17      * 
     18      * 定义含有泛型的方法:调用方法时确定数据类型
     19      *         修饰符 <定义泛型> 返回值类型 方法名(参数<使用泛型>){
     20      *             方法体
     21      *         }
     22      * 
     23      * 
     24      * 方法的参数:
     25      *         Class clazz
     26      *         Map<String,String[]> properties
     27      * 方法的返回值类型:
     28      *         Object
     29      */
     30     public static <E> E populate03(Class<E> clazz, Map<String,String[]> properties){
     31         try {
     32             //2.内部通过反射创建Javabean对象
     33             E obj = clazz.newInstance();
     34             //3.调用BeanUtils工具类的方法populate
     35             BeanUtils.populate(obj, properties);
     36             //5.把对象返回给用户
     37             return obj;
     38         } catch (Exception e) {
     39             //4.对populate方法的异常进行try...catch处理
     40             e.printStackTrace();
     41             //把编译异常,转换为运行时异常,给成员变量注入值失败,让程序停止下来
     42             throw new RuntimeException("注入值失败");
     43         }
     44     }
     45     
     46     /*
     47      * 定义一个方法(让用户使用自己定义的populate方法不用处理异常)
     48      *     1.参数传递JavaBean对象的Class文件对象
     49      *     2.内部通过反射创建Javabean对象
     50      *     3.调用BeanUtils工具类的方法populate
     51      *     4.对populate方法的异常进行try...catch处理
     52      *     5.把对象返回给用户
     53      * 
     54      * 方法的参数:
     55      *         Class clazz
     56      *         Map<String,String[]> properties
     57      * 方法的返回值类型:
     58      *         Object
     59      */
     60     public static Object populate02(Class clazz, Map<String,String[]> properties){
     61         try {
     62             //2.内部通过反射创建Javabean对象
     63             Object obj = clazz.newInstance();
     64             //3.调用BeanUtils工具类的方法populate
     65             BeanUtils.populate(obj, properties);
     66             //5.把对象返回给用户
     67             return obj;
     68         } catch (Exception e) {
     69             //4.对populate方法的异常进行try...catch处理
     70             e.printStackTrace();
     71             //把编译异常,转换为运行时异常,给成员变量注入值失败,让程序停止下来
     72             throw new RuntimeException("注入值失败");
     73         }
     74         
     75     }
     76     
     77     /*
     78      * 定义一个方法(让用户使用自己定义的populate方法不用处理异常)
     79      *     1.调用BeanUtils工具类的方法populate
     80      *     2.对populate方法的异常进行try...catch处理
     81      * 
     82      * 方法的参数:
     83      *         Object bean
     84      *         Map<String,String[]> properties
     85      * 方法的返回值类型:
     86      *         void
     87      */
     88     public static void populate01(Object bean, Map<String,String[]> properties){
     89         try {
     90             //1.调用BeanUtils工具类的方法populate
     91             BeanUtils.populate(bean, properties);
     92         } catch (Exception e) {
     93             //2.对populate方法的异常进行try...catch处理
     94             e.printStackTrace();
     95             //把编译异常,转换为运行时异常,给成员变量注入值失败,让程序停止下来
     96             throw new RuntimeException("注入值失败");
     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  * 使用自定义工具类MyBeanUtils
    111  */
    112 public class UseMyBeanUtils {
    113     @Test
    114     public void demo03(){
    115         //创建Map集合,key是String类型,value是String类型的数组
    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[]{"吃","睡","玩","敲代码"});
    121         //调用MyBeanUtils工具类中的方法populate02
    122         User u = MyBeanUtils.populate03(User.class, properties);
    123         System.out.println(u);
    124     }
    125     
    126     @Test
    127     public void demo02(){
    128         //创建Map集合,key是String类型,value是String类型的数组
    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[]{"吃","睡","玩","敲代码"});
    134         //调用MyBeanUtils工具类中的方法populate02
    135         User u = (User) MyBeanUtils.populate02(User.class, properties);
    136         System.out.println(u);
    137     }
    138     
    139     @Test
    140     public void demo01(){
    141         //创建JavaBean对象
    142         User u = new User();
    143         //创建Map集合,key是String类型,value是String类型的数组
    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[]{"吃","睡","玩","敲代码"});
    149         //调用MyBeanUtils工具类中的方法populate01
    150         MyBeanUtils.populate01(u, properties);
    151         System.out.println(u);
    152     }
    153 }
     1 /*
     2  * 综合案例:XML+dom4j+反射+BeanUtils
     3  *     1.使用xml存储JavaBean的全类名和属性名属性值
     4  *     2.使用dom4j解析xml
     5  *     3.使用反射技术根据解析出的全类名创建JavaBean对象
     6  *     4.使用BeanUtils中的方法setProperty给成员变量注入值
     7  */
     8 public class UseDom4jparseXMLToJavaBean {
     9     public static void main(String[] args) throws Exception {
    10         //2.使用dom4j解析xml
    11         //获取dom4j的核心类SAXReader
    12         SAXReader sax = new SAXReader();
    13         //使用SAXReader中的read读取xml,创建Document对象
    14         Document docu = sax.read("src/cn/itcast/dmeo05/domain/data.xml");
    15         //使用Document中的方法getRootElement获取根元素
    16         Element rootElement = docu.getRootElement();
    17         //使用Element中的方法elements获取所有的bean元素,放入集合中
    18         List<Element> beanElementList = rootElement.elements();
    19         //遍历beanElementList集合
    20         for (Element beanElement : beanElementList) {
    21             //获取beanElement上的变的属性className
    22             String className = beanElement.attributeValue("className");
    23             //3.使用反射技术根据解析出的全类名创建JavaBean对象
    24             Class clazz = Class.forName(className);
    25             Object obj = clazz.newInstance();
    26             //使用Element中的方法elements获取所有的property元素,放入集合中
    27             List<Element> propertyElementList = beanElement.elements();
    28             //遍历propertyElementList集合
    29             for (Element propertyElement : propertyElementList) {
    30                 //获取propertyElement上的属性name(属性名)和value(属性值)
    31                 String name = propertyElement.attributeValue("name");
    32                 String value = propertyElement.attributeValue("value");
    33                 //4.使用BeanUtils中的方法setProperty给成员变量注入值
    34                 BeanUtils.setProperty(obj, name, value);
    35             }
    36             //打印JavaBean对象
    37             System.out.println(obj);
    38         }
    39     }
    40 
    41 }
  • 相关阅读:
    java提高篇(二四)-----HashSet
    链表(线性表)
    逆置线性表(线性表)
    Android布局_表格布局TableLayout
    Android布局_布局概述和LinearLayout布局
    Android_用户界面概述和数据单位
    Android_SDK的常用命令
    Android_程序结构分析
    CSS3_边框属性之圆角的基本图形案例
    CSS3_边框属性之圆角
  • 原文地址:https://www.cnblogs.com/caigq/p/7049170.html
Copyright © 2011-2022 走看看