Java:XML&约束&注解

2023-06-27  本文已影响0人  iOS_修心

一、xml

1.概述

2.标签规则

3、语法规则

二、约束

三、DTD

1、 编写DTD约束

2、引入DTD约束

3、DTD语法

定义元素

定义一个元素的格式为:<!ELEMENT 元素名 元素类型>

![04_schema约束介绍.png](https://img.haomeiwen.com/i1969836/6ee53b881193688c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) DTD语法定义元素.png
定义属性

定义一个属性的格式为:<!ATTLIST 元素名称 属性名称 属性的类型 属性的约束>

    <!ELEMENT persons (person+)>
    <!ELEMENT person (name,age)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT age (#PCDATA)>
    <!ATTLIST person id CDATA #REQUIRED>

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE persons SYSTEM 'persondtd.dtd'>

    <persons>
        <person id="001">
            <name>张三</name>
            <age>23</age>
        </person>

        <person id = "002">
            <name>张三</name>
            <age>23</age>
        </person>
    </persons>

四、schema约束

1、编写schema约束

schema约束编写.png
<?xml version="1.0" encoding="UTF-8" ?>
    <schema
        xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.itheima.cn/javase"
        elementFormDefault="qualified"
    >

        <!--定义persons复杂元素-->
        <element name="persons">
            <complexType>
                <sequence>
                    <!--定义person复杂元素-->
                    <element name = "person">
                        <complexType>
                            <sequence>
                                <!--定义name和age简单元素-->
                                <element name = "name" type = "string"></element>
                                <element name = "age" type = "string"></element>
                            </sequence>
                        <!--定义属性,required( 必须的)/optional( 可选的)-->
                            <attribute name="id" type="string" use="required"></attribute>
                        </complexType>
                    </element>
                </sequence>
            </complexType>
        </element>
    </schema>

2、引入schema约束

  1. 在根标签上定义属性xmlns="http://www.w3.org/2001/XMLSchema-instance"

  2. 通过xmlns引入约束文件的名称空间

  3. 给某一个xmlns属性添加一个标识,用于区分不同的名称空间
    xmlns:标识=“名称空间地址” ,标识可以是任意的,但是一般取值都是xsi

  4. 通过xsi:schemaLocation指定名称空间所对应的约束文件路径
    xsi:schemaLocation = "名称空间url 文件路径“

    <?xml version="1.0" encoding="UTF-8" ?>

    <persons
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.itheima.cn/javase"
        xsi:schemaLocation="http://www.itheima.cn/javase person.xsd"
    >
        <person>
            <name>张三</name>
            <age>23</age>
        </person>

    </persons>

五、注解

1、 概述

2、自定义注解

  public @interface 注解名称 {
    public 属性类型 属性名() default 默认值 ;
  }
//表示Test这个注解的存活时间
    @Retention(value = RetentionPolicy.RUNTIME)
    public @interface Test {
    }

    public class UseTest {

        //没有使用Test注解
        public void show(){
            System.out.println("UseTest....show....");
        }

        //使用Test注解
        @Test
        public void method(){
            System.out.println("UseTest....method....");
        }

        //没有使用Test注解
        @Test
        public void function(){
            System.out.println("UseTest....function....");
        }
    }

    public class AnnoDemo {
        public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
            //1.通过反射获取UseTest类的字节码文件对象
            Class clazz = Class.forName("com.itheima.myanno3.UseTest");

            //创建对象
            UseTest useTest = (UseTest) clazz.newInstance();

            //2.通过反射获取这个类里面所有的方法对象
            Method[] methods = clazz.getDeclaredMethods();

            //3.遍历数组,得到每一个方法对象
            for (Method method : methods) {
                //method依次表示每一个方法对象。
                //isAnnotationPresent(Class<? extends Annotation> annotationClass)
                //判断当前方法上是否有指定的注解。
                //参数:注解的字节码文件对象
                //返回值:布尔结果。  true 存在  false 不存在
                if(method.isAnnotationPresent(Test.class)){
                    method.invoke(useTest);
                }
            }
        }
    }

3、元注解

  @Target({ElementType.FIELD,ElementType.TYPE,ElementType.METHOD})  //指定注解使用的位置(成员变量,类,方法)
  @Retention(RetentionPolicy.RUNTIME) //指定该注解的存活时间
  //@Inherited //指定该注解可以被继承
  public @interface Anno {
  }

  @Anno
  public class Person {
  }

  public class Student extends Person {
      public void show(){
          System.out.println("student.......show..........");
      }
  }

  public class StudentDemo {
      public static void main(String[] args) throws ClassNotFoundException {
          //获取到Student类的字节码文件对象
          Class clazz = Class.forName("com.itheima.myanno4.Student");

          //获取注解。
          boolean result = clazz.isAnnotationPresent(Anno.class);
          System.out.println(result);
      }
  }
上一篇 下一篇

猜你喜欢

热点阅读