xml--概念引进
发布时间:2020-12-16 23:44:42 所属栏目:百科 来源:网络整理
导读:XML 的使用场景 : 充当Java程序的 配置文件 . 书写一个xml配置文件 ?xml version="1.0" encoding="UTF-8" ?!-- xml文件必须有一个根标签,仅一个 --store !--存储数据有两种方式,一: 标签体中. 二: 标签头部的属性中-- product category="手机数码" pid100/pi
XML 的使用场景 : 充当Java程序的
配置文件 .
书写一个xml配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!-- xml文件必须有一个根标签,仅一个 --> <store> <!--存储数据有两种方式,一: 标签体中. 二: 标签头部的属性中--> <product category="手机数码"> <pid>100</pid> <pname>华为P20</pname> <price>8888</price> <!--如果标签体中没有数据,数据在标签头的属性中,该标签可以书写为自关闭标签--> <property name="username" value="张三"></property> <property name="password" value="123456" /> </product> <product category="电脑办公"> <pid>200</pid> <pname>外星人NBility</pname> <price>18888</price> </product> <product category="大型家电"> <pid>300</pid> <pname>海尔全自动洗衣机</pname> <price>666</price> </product> </store> 约束介绍 (DTD,Schema) <!DOCTYPE books[ <!ELEMENT books (book+)> <!ELEMENT book (name,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ATTLIST book author CDATA #REQUIRED> <!-- #IMPLIED 可选 --> ]> xml 文件编写 : <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE books[ <!ELEMENT books (book+)> <!ELEMENT book (name,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ATTLIST book author CDATA #REQUIRED> <!-- #IMPLIED 可选 --> ]> <books> <book author="张三丰"> <name>Java从入门到入土</name> <price>19888</price> </book> <book author="灭绝师太"> <name>Java编程思想</name> <price>98</price> </book> </books> Schema 约束 : XML Scheman <!-- 传智播客 Schema 教学实例文档 : 模拟 spring 框架规范 : 如果开发人员需要在 xml 使用当前 schema 约束,必须包含指定命名空间. 格式如下 : <beans xmlns="http://www.example.org/bean-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/bean-schema bean-schema.xsd"> xml schema defination </beans> --> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/bean-schema" xmlns:tns="http://www.example.org/bean-schema" elementFormDefault="qualified"> <!-- 声明根标签 --> <element name="beans"> <complexType> <sequence> <element name="bean" minOccurs="0" maxOccurs="unbounded"> <complexType> <sequence> <element name="property" minOccurs="0" maxOccurs="unbounded"> <complexType> <attribute name="name" type="string" use="required"></attribute> <attribute name="value" type="string" use="required"></attribute> </complexType> </element> </sequence> <attribute name="id" type="string" use="required"></attribute> <attribute name="className" type="string" use="required"></attribute> </complexType> </element> </sequence> </complexType> </element> </schema> xml 文件编写 : <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.example.org/bean-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/bean-schema bean-schema.xsd"> <bean id="001" className="cn.itcast.bean.User" > <property name="username" value="Peter" /> <property name="password" value="123456" /> </bean> <bean id="002" className="cn.itcast.bean.User"> <property name="username" value="张三"></property> <property name="password" value="654321"></property> </bean> </beans> XML解析 Dom4j (思想: 文档树思想) 将 xml 文件读取到内存,形成一个文档树,然后进行解析. @Test // 1. 创建一个 SAXReader 对象 SAXReader saxReader = new SAXReader(); // 2. 调用 read 方法,将 xml 读取到内存,形成一颗文档树 Document document = saxReader.read("books.xml"); // 3. 获取根标签 Element root = document.getRootElement(); // 4. 获取子标签 List<Element> bookElements = root.elements(); // 5. 遍历 for (Element book : bookElements) { // 6. 获取属性 String author = book.attributeValue("author"); System.out.println("author = " + author); // 7. 获取子标签 List<Element> elements = book.elements(); // 8. 遍历子标签 for (Element element : elements) { // 9. 获取名称和文本 String name = element.getName(); String text = element.getText(); System.out.println(name + " = " + text); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c – 仅通过公共继承暴露某些方法
- SQLLite (二) :sqlite3_open, sqlite3_exec, slite3_close
- objective-c – 是否可以在iOS中对一个目录进行全局操作并将
- React Native App的热部署
- LoadRunner测试Flex
- swift – 导致’在初始化之前被闭包捕获的常量’错误的原因
- 3520a mmz错误解决方法
- oracle正则函数,regexp_substr,按分隔符单行转多行
- Flex 学习之路之九 用户接口user interface includeInLayou
- typeHandler接口实现FastJson中的JSONObject