XML

    xiaoxiao2021-04-11  45

    1、XML : extensible markup language 可扩展标记语言 version="1.0" 可扩展:所有的标签都是自定义的功能:数据存储 配置文件数据传输 html 与 xml 区别 HTML语法松散,xml语法严格html做页面展示,xml做数据存储Html所有标签都是预定义的,xml所有标签都是自定义的 2、xml语法: 文档声明 必须写在xml文档的第一行写法:<?xml version = "1.0"?>属性 version 版本号 固定值1.0encoding 指定文档的码表 默认值为iso-8859-1standalone 指定文档是否独立 yes 或 no 元素 xml文档中的标签 文档中必须有且只能有一个根元素元素需要正确闭合 <body></body> <br/>元素需要正确嵌套元素名称要遵守 元素名称区分大小写数字不能开头 文本 转义字符 >;CDATA 里边的数据会原样显示 <!CDATA[数据内容]> 属性 属性值必须用引号引起来,单双引号都行 注释 <!-- --> 处理指令:现在基本不用 <?xml-stylesheet type="text/css" href="1.css"?> 3、XML约束 约束就是xml的书写规则约束的分类: dtd  约束不严谨 内部dtd 在xml内部定义dtd外部dtd 在外部文件中定义dtd 本地dtd文件 <!DOCTYPE students SYSTEM "student.dtd">网络dtd文件 <!DOCTYPE students PUBLIC "名称空间" “student.dtd”> Student.dtd <!ELEMENT students (student*) ><!ELEMENT student (name,age,sex)><!ELEMENT name (#PCDATA)><!ELEMENT age (#PCDATA)><!ELEMENT sex (#PCDATA)><!ATTLIST student number ID #REQUIRED> 唯一的,必须的 student.xml <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE students SYSTEM  "student.dtd"><students>    <student number="s0001" >        <name>zs</name>        <age>abc</age>        <sex>yao</sex>    </student></students> schema 导入xsd约束文档编写根标签引入实例名称空间 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"引入名称空间 xsi:schemaLocation="http://www.itcast.cn/xml student.xsd"引入默认的名称空间student.xsd <?xml version="1.0"?><xsd:schema xmlns="http://www.itheima.cn/xml"        xmlns:xsd="http://www.w3.org/2001/XMLSchema"        targetNamespace="http://www.itheima.cn/xml" elementFormDefault="qualified">    <xsd:element name="students" type="studentsType"/>    <xsd:complexType name="studentsType">        <xsd:sequence>            <xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>        </xsd:sequence>    </xsd:complexType>    <xsd:complexType name="studentType">        <xsd:sequence>            <xsd:element name="name" type="xsd:string"/>            <xsd:element name="age" type="ageType" />            <xsd:element name="sex" type="sexType" />        </xsd:sequence>        <xsd:attribute name="number" type="numberType" use="required"/>    </xsd:complexType>    <xsd:simpleType name="sexType">        <xsd:restriction base="xsd:string">            <xsd:enumeration value="male"/>            <xsd:enumeration value="female"/>        </xsd:restriction>    </xsd:simpleType>    <xsd:simpleType name="ageType">        <xsd:restriction base="xsd:integer">            <xsd:minInclusive value="0"/>            <xsd:maxInclusive value="256"/>        </xsd:restriction>    </xsd:simpleType>    <xsd:simpleType name="numberType">        <xsd:restriction base="xsd:string">            <xsd:pattern value="itheima_\d{4}"/>        </xsd:restriction>    </xsd:simpleType></xsd:schema> student.xml <?xml version="1.0" encoding="UTF-8" ?><!--    1、编写根标签    2、引入实例名称空间 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    3、引入名称空间 xsi:schemaLocation="http://www.itcast.cn/xml student.xsd"       4、引入默认的名称空间 --><students    xmlns="http://www.itheima.cn/xml"    xsi:schemaLocation="http://www.itheima.cn/xml student.xsd"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    <student number="itheima_1001">        <name>asfd</name>        <age>12</age>        <sex>male</sex>    </student></students><students    xmlns:itheima="http://www.itheima.cn/xml"    xsi:schemaLocation="http://www.itheima.cn/xml student.xsd"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    <itheima:student number="itheima_1001">        <itheima:name>asfd</itheima:name>        <itheima:age>12</itheima:age>        <theima:sex>male</itheima:sex>    </itheima:student></itheima:students> 4、XML解析 解析XML可以做: 如果xml作为配置文件:读取如果xml作为传输文件:写、读 XML解析思想: DOM:将文档加载到内存,形成一棵dom树(document对象),将文档的各个组成部分封装为一些对象 优点:因为在内存中会形成dom树,可以对dom树进行增删改查缺点:dom树非常占内存,解析速度慢Document Element Text Attribute Comment SAX:逐行读取,基于事件驱动 优点:不占内存,速度快缺点:只能读取,不能回写 xml常用的解析器 JAXP sun公司提供的解析 支持dom和saxJDOMDOM4J dom for java民间方式,但是是事实方式,非常好,支持dom 导入jar包 dom4j.jar创建解析器 SAXReader reader = new SAXReader() 解析xml 获得document对象 Document document = reader.read(url) 解析xml public class TestDom4j {        @Test        public void test1() throws Exception{              //创建一个xml解析对象             SAXReader reader = new SAXReader();              //把xml文档加载到document对象中             Document document = reader .read( "src/Book.xml" );             Element root = document .getRootElement(); //          Element bookNode = root.element("书"); //          System.out.println(bookNode.getName());              //得到当前节点所有的子节点             List list = root .elements();              //得到第二本书对象             Element secondBook = (Element) list .get(1);              //得到当前节点的文本内容             String name = secondBook .element( "书名" ).getText();             System. out .println( name );       }               @Test        //遍历所有元素节点        public void test2() throws Exception{              //创建一个xml解析对象             SAXReader reader = new SAXReader();              //把xml文档加载到document对象中             Document document = reader .read( "src/Book.xml" );             Element root = document .getRootElement();             treeWalk( root );       }               private void treeWalk(Element ele ){              //输出当前节点的名字             System. out .println( ele .getName());              //ele.nodeCount()得到当前节点的所有子节点的数量              for ( int i = 0; i < ele .nodeCount(); i ++){                    //取出下标为i的节点                   Node node = ele .node( i );                    //判断当前节点是否为标签                    if ( node instanceof Element){                          //把node强转为标签(Element)                         treeWalk((Element) node );                   }             }       } } XPATH 专门用于查询 定义了一种规则使用方法 selectSingleNode()selectNodes() 使用步骤 注意:要导包 jaxen...jar创建解析器 SAXReader reader = new SAXReader()解析xml 获得document对象 Document document = reader.read(url) XPath:         //    nodename 选取此节点。        //    /     从根节点选取。        //    //     从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。        //    ..     选取当前节点的父节点。        //    @     选取属性。        //      [@属性名]    属性过滤        //      [标签名]     子元素过滤 public class TestXPath2 {        @Test        public void test() throws Exception{             SAXReader read = new SAXReader();             Document document = read .read( "src/Dom4jTest.xml" );             List nodes = document .selectNodes( "/bookstore//book/title" );              for ( int i = 0; i < nodes .size(); i ++) {                   Node node = (Node) nodes .get( i );                   System. out .println( node .getText());             }       } }
    转载请注明原文地址: https://ju.6miu.com/read-666816.html

    最新回复(0)