XML知识点(总共2部分)--2

继上一篇博客

3、xml约束

  • A:xmlns(xml nameSpace)命名空间:元素,属性名称的集合,解决元素相同名称冲突问题

    • 使用命名空间注意:

      1. 命名空间前缀,作用在元素的开始标签上,该元素的所有子元素和属性都在该命名空间;

        <h:student xmlns:h="http://briup.com/h"></h:student>
        
      2. xmlns定义默认命名空间:默认命名空间只作用于元素,不作用于属性;为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。

      3. 根元素的开始标签:定义命名空间前缀(a、多个命名空间前缀不能重复,b、多个命名空间可以指向同一个URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。

        <root 
        	xmlns:h="http://briup.com/h"
        	xmlns:s="http://briup.com/s" >
            <!-- http://briup.com相同的URL -->
        	
        		<h:student></h:student>
        		<s:student></s:student>
        </root>
        
      4. 判断元素,属性相同时:a、命名空间前缀,b、元素名/c、属性名,d、空间前缀所指向的URL

  • B:Schema约束:作用用来对xml文件内容进行约束;

    • schema约束:使用了标签对xml中的标签进行约束

      • a、提供了内置的数据类型;
      • b、还可以用户自定义Simple Type(简单类型)Complex type(复杂类型);
      • c、schema文件的后缀名为 .xsd(xml schema document)。
    • schema约束文件:.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <schema 
    //1、xmlns默认的命名空间
            xmlns="http://www.w3.org/2001/XMLSchema"
    //2、schema约束作用在targetNamespace命名空间上
    		targetNamespace="http://www.example.org/test7"
    //3、命名空间前缀tns属于targetnamespace指定的名称空间
    		xmlns:tns="http://www.example.org/test7"
    //4、指定schema文档中声明的根元素和所有子元素都属于targetnamespace指定的名称空间
    		elementFormDefault="qualified">
      
    //5、element定义xml文件中出现的标签(标签名),定义了type属性后,不可定义子标签
    	<element name="root">    
    	//6、complexType约束element定义的xml的root标签文本域类型为complexType
    		<complexType>
    		//7、必需sequence序列:element标签的顺序定义了xml文件出现的标签顺序
    			<sequence>
                //8、minOccus:限定元素最少出现次数;maxOccus:限定元素最多出现次数
                //9、<element name="标签名" type="string" minOccurs="1" maxOccurs="1"></element>
                    //10、name:xml文件中必须出现的标签名
                    //11、type:xml中标签的文本域类型(例如:int simpleType Complextype)
                    //12.tns:当前schema文件下的命名空间前缀
    				<element name="stu" type="tns:mystu"></element> 
    				<element name="tea" type="tns:mytea"></element>
                    
    				<element name="course">
    					<complexType>
                        //13、attribute标签定义xml中course标签的
                        //14、xml中course标签的属性名name(例如:id)
                       	//15、xml中course标签的属性值类型type(例如:string)
                        //16、xml中course标签的属性use:optional:表示属性可有可无;required:表示属性必须要有;prohibited:禁止的
    						<attribute name="id" type="string" use="optional" default="10"></attribute>
    						<attribute name="name" type="tns:mystu" use="optional" default="大学生"></attribute>
    					</complexType>
    				</element>
                    
    			</sequence>
    		</complexType>
    	</element>
        
    //自定义简单类型
    //element标签定义xml中出现的标签
    //element标签属性type为simpleType,simpleType名为mytea
    	<simpleType name="mytea">
        //restriction限制标签
    	//element标签定义xml中出现的标签,xml中标签的文本域int类型
    		<restriction base="int">
            //文本域为int类型,文本域最大值为 10;最小值为 3;(包含空格 换行)
    			<minInclusive value="3"></minInclusive>
    			<maxInclusive value="10"></maxInclusive>
    		</restriction>
    	</simpleType>
        
    //自定义简单类型
    	<simpleType name="mystu">
    		<restriction base="string">
            //element标签属性type为mystu对应xml中的标签文本域只能枚举类型(大学生|小学生)
    			<enumeration value="小学生"></enumeration>
    			<enumeration value="大学生"></enumeration>
    		</restriction>
    	</simpleType>
        
    </schema>
    
    • schema约束的xml文件:

      <?xml version="1.0" encoding="UTF-8"?>
      //1、xml默认命名空间
      <root xmlns="http://www.example.org/test7"
            
      	//2、schema文件默认命名空间对应约束的xml实例
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            
          //3、xsi:schemaLocation="{namespace} {location}"
          //4、xml实例文件默认命名空间指定schema约束文件位置
      	xsi:schemaLocation="http://www.example.org/test7 test7.xsd">
      
          //5、schema文件中的element标签;attribute属性标签;simpleType,complexType类型标签;sequence顺序标签;约束
      	<stu>大学生</stu>
      	<tea>
      		7
      	</tea>
      	<course id="10" name="大学生"></course>
      </root>
      

4、xml解析:Dom4j解析

  • A:Dom4j(Document For Java):第三方工具包,获得解析器解析xml,将xml文件解析为树结构。

    • 怎么引入jar包?
    • 1.1首先需要有一个java program ,然后左键单击选中项目,右键new一个Folder取名为jar
    • 1.2然后把想引入的jar包复制到刚才新建的folder下面
    • 1.3选中想依赖到项目中的jar包,右键 选中build Path 然后点击 add to build Path
    • 1.4 依赖完成 —>jar包已经添加到项目中
  • B:解析xml文件的步骤:

    //1、获得解析器
    SAXReader sr = new SAXReader();
    //2、解析器解析对应的xml文件,指定xml文件路径
    Document doc = sr.read(new File("src/com/briup/xml/d2/xx.xml"));//file文件路径是当前项目下的,要写明路径
    //3、获取xml根节点root
    Element root = doc.getRootElement();
    //4、获取根元素的所有字元素(不包括子节点的子节点)
    List<Element> children = root.elements();
    //41、遍历获取根元素的子元素
    for(Element child:children) {
    	//42、获取子元素中所有的子元素
    	List<Element> son = child.elements();
    	//43、获取该子元素的所有属性
    	List<Attribute> aList = child.attributes();
    	//44、获得该子元素指定属性名的属性
    	Attribute attribute = child.attribute("属性名");
    	//45、获得该元素的指定属性名的属性值
    	String childValue = child.attributeValue("属性名");
    	//46、获得该属性的属性值
    	String value = attribute.getValue();
    }
    //5、获得根元素的文本域(包括换行 空格)
    String text = root.getText();
    //6、获得很元素以及根元素的子元素的文本域(包括换行 空格)
    String stringText = root.getStringValue();
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值