本文主要记录xml学习过程中的一些笔记,包括xml作用,语法以及解析。
1、HTML和XML的区别
1.1、HTML
名称: HyperText Markup Languae(超文本标记语言)
标签: 标签是w3c组成指定,固定的,约100来个。也可以自定义标签。(自定义标签也可叫xml标签)
作用: 负责网页的结构 。被设计用来显示数据。
HTML: 负责网页的结构
CSS: 负责网页的样式(美观)
Javascript: 负责在浏览器端与用户进行交互。
负责静态的网页制作的语言。HTML语言特点:
1)由标签组成。
<p> <hr/> <br/>等。</p>
<p style="margin-left: 60px">2)语法结构松散的。表现在:大小写不区分,结束标签和开始标签不一定匹配。</p>
<h2>1.2、XML </h2>
<p style="margin-left: 60px">名称:<strong>E<span style="color: rgba(255, 0, 0, 1)">x</span>tend <span style="color: rgba(255, 0, 0, 1)">M</span>arkup<span style="color: rgba(255, 0, 0, 1)"> L</span>anguge</strong>(可扩展标签语言)。是一种标记语言,很类似 HTML</p>
<p style="margin-left: 60px">标签:标签由开发者自己制定的(要按照一定的语法定义),没有被预定义。</p>
<p style="margin-left: 60px">作用:设计宗旨是<strong><span style="color: rgba(255, 0, 0, 1)">传输数据</span></strong>,而非显示数据。被设计为具有自我描述性。</p>
<ul>
<li style="margin-left: 60px">描述带关系的数据(作为软件的配置文件): 包含与被包含的关系。</li>
<li style="margin-left: 60px">作为数据的载体(存储数据,小型的"数据库")</li>
</ul>
<h1>2 XML作用</h1>
<h2>2.1 描述带关系的数据(<span style="color: rgba(255, 0, 0, 1)">软件的配置文件</span>)</h2>
<p> web服务器(PC):</p>
<p> 使用ip(255.43.12.54)地址和端口(1521),连接oracle数据库,保存学生数据。</p>
<p> 把ip地址端口配置到xml文件:</p>
<p> host.xml</p>
<p> <span style="color: rgba(255, 0, 0, 1)"><host> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> <ip>255.43.12.55</ip> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> <port>1521</port> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> </host></span></p>
<h2>2.2 数据的载体(小型的"数据库")</h2>
<p> 教师管理系统: 教师信息:姓名+工龄+邮箱</p>
<p> 当发送教师数据给财务管理系统时,可以有多种发送方式,如:</p>
<ol>
<li>字符串发送:String teacher = name=张三&email=zhangsan@qq.com&workage=2 。此法存在问题: 1)不好解析 2)不是规范)。</li>
<li> xml格式发送:</li>
</ol>
<p> <span style="color: rgba(255, 0, 0, 1)"> teacher.xml </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> <teacher> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> <name>张三</name> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> <email>zhangsan@qq.com</email> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> <workage>2</workage> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> </teacher> </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> 这种一种规范</span></p>
<h1>3 XML语法</h1>
<p> xml文件以xml后缀名结尾。</p>
<p> xml文件需要使用xml解析器去解析。浏览器内置了xml解析器。</p>
<h2>3.1 标签</h2>
<p> 语法: <student></student> 开始标签 标签体内容 结束标签</p>
<p> 1)<student/> 或 <student></student> 空标签。没有标签体内容</p>
<p> 2)<strong><span style="color: rgba(255, 0, 0, 1)">xml标签名称区分大小写</span></strong>。</p>
<p> 3)xml标签一定要<strong><span style="color: rgba(255, 0, 0, 1)">正确配对</span></strong>。</p>
<p> 4)xml标签名中间不能使用空格。</p>
<p> 5)xml标签名不能以数字开头</p>
<p> <span style="color: rgba(255, 0, 0, 1)"> 6)注意: 在一个xml文档中,有且仅有一个根标签 </span></p>
<h2>3.2 属性</h2>
<p> 语法: <Student<span style="color: rgba(255, 0, 0, 1)"> name="eric"</span>>student</Student></p>
<p> 注意:</p>
<p> 1)<strong>属性值必须以引号包含</strong>,不能省略,也不能单双引号混用!!!</p>
<p> 2)一个标签内<strong>可以有多个属性</strong>,但不能出现重复的属性名!!!</p>
<h2>3.3 注释</h2>
<p> 语言: <!-- xml注释 --> 。在 XML 中编写注释的语法与 HTML 的语法很相似:</p>
<pre><!-- This is a comment --> </pre>
<h2>3.4 xml文档声明</h2>
<p></p>
<p style="margin-left: 60px">语法: <?xml version="1.0" encoding="utf-8"?></p>
<p style="margin-left: 60px"> version: xml的版本号</p>
<p style="margin-left: 60px"> encoding: 解析xml文件时查询的码表(解码过程时查询的码表)</p>
<p style="margin-left: 60px"> 注意:</p>
<p style="margin-left: 60px"> 1)如果在ecplise工具中开发xml文件,保存xml文件时自动按照文档声明的encoding来保存文件。</p>
<p style="margin-left: 60px"> 2)如果用记事本工具修改xml文件,注意保存xml文件按照文档声明的encoding的码表来保存。</p>
<h2>3.5 转义字符/实体引用</h2>
<p> 在xml中内置了一些特殊字符,这些特殊字符不能直接被浏览器原样输出。如果希望把这些特殊字符按照原样输出到浏览器,对这些特殊字符进行转义。转义之后的字符就叫转义字节。</p>
<p>如果你把字符 "<" 放在 XML 元素中,会发生错误,这是因为解析器会把它当作新元素的开始。</p>
<p style="margin-left: 30px">这样会产生 XML 错误:</p>
<p style="margin-left: 60px"><message>if salary < 1000 then</message></p>
<p style="margin-left: 30px">为了避免这个错误,请用实体引用来代替 "<" 字符:</p>
<pre><message>if salary <code><</code> 1000 then</message> </pre>
<p style="margin-left: 30px">在 XML 中,有 5 个预定义的实体引用:</p>
<table class="dataintable" style="height: 164px; width: 581px">
<tbody>
<tr>
<td><span style="font-size: 18px"><strong><</strong></span></td>
<td><span style="font-size: 18px"><strong><</strong></span></td>
<td><span style="font-size: 18px"><strong>小于</strong></span></td>
</tr>
<tr>
<td><span style="font-size: 18px"><strong>></strong></span></td>
<td><span style="font-size: 18px"><strong>></strong></span></td>
<td><span style="font-size: 18px"><strong>大于</strong></span></td>
</tr>
<tr>
<td><span style="font-size: 18px"><strong>&</strong></span></td>
<td><span style="font-size: 18px"><strong>&</strong></span></td>
<td><span style="font-size: 18px"><strong>和号</strong></span></td>
</tr>
<tr>
<td><span style="font-size: 18px"><strong>'</strong></span></td>
<td><span style="font-size: 18px"><strong>'</strong></span></td>
<td><span style="font-size: 18px"><strong>单引号</strong></span></td>
</tr>
<tr>
<td><span style="font-size: 18px"><strong>"</strong></span></td>
<td><span style="font-size: 18px"><strong>"</strong></span></td>
<td><span style="font-size: 18px"><strong>引号</strong></span></td>
</tr>
</tbody>
</table>
<p class="note" style="margin-left: 30px">注释:在 XML 中,只有字符 "<" 和 "&" 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。 </p>
<h2>3.6 CDATA块</h2>
<p> 作用: 可以让一些需要进行包含特殊字符的内容统一进行原样输出。</p>
<h2>3.7 处理指令</h2>
<p> 作用: 告诉xml解析如果解析xml文档</p>
<p> 案例: <?xml-stylesheet type="text/css" href="1.css"?> 告诉xml解析该xml文档引用了哪个css文件</p>
<p> 需要提取xml内容可以使用xml-stylesheet指令指令</p>
<h1>4 XML解析 </h1>
<h2>4.1 引入</h2>
<p> xml文件除了给开发者看,更多的情况使用<span style="color: rgba(255, 0, 0, 1)">程序读取xml文件的内容</span>。这叫做xml解析</p>
<h2> 4.2 XML解析方式(原理不同)</h2>
<p> DOM解析</p>
<p> SAX解析</p>
<h2>4.3 XML解析工具</h2>
<p> DOM解析原理:</p>
<p> 1)JAXP (oracle-Sun公司官方)</p>
<p> 2)JDOM工具(非官方)</p>
<p> <span style="color: rgba(255, 0, 0, 1)"> 3)Dom4J工具(非官方) </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> java三大框架(默认读取xml的工具就是Dom4j) </span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> .......</span></p>
<p><span style="color: rgba(255, 0, 0, 1)"> </span>SAX解析原理:</p>
<p> <span style="color: rgba(255, 0, 0, 1)"> 1)Sax解析工具(oracle-sun公司官方)</span></p>
<h2>4.4 什么是DOM解析</h2>
<p> DOM解析原理:xml解析器一次性把整个xml文档加载进内存,然后在内存中构建一颗Document的对象树,通过Document对象,得到树上的节点对象,通过节点对象访问(操作)到xml文档的内容。</p>
<h2>4.5 Dom4j工具</h2>
<p> 非官方,不在java jdk中。</p>
<p> 使用步骤:</p>
<p> 1)导入dom4j的核心包。 dom4j-1.6.1.jar</p>
<p> 2)编写Dom4j读取xml文件代码</p>
<table style="border-collapse: collapse" border="0"><colgroup><col style="width: 590px"></colgroup>
<tbody valign="top">
<tr>
<td style="padding-left: 7px; padding-right: 7px; border: 0.5pt solid rgba(0, 0, 0, 1)">
<p><span style="color: rgba(127, 0, 85, 1); font-family: Courier New"><strong>public</strong><span style="color: rgba(0, 0, 0, 1)"> <span style="color: rgba(127, 0, 85, 1)"><strong>static</strong><span style="color: rgba(0, 0, 0, 1)"> <span style="color: rgba(127, 0, 85, 1)"><strong>void</strong><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {</span> </span></span></span></span></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-family: Courier New"> <span style="color: rgba(127, 0, 85, 1)"><strong>try</strong><span style="color: rgba(0, 0, 0, 1)"> {</span> </span></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><span style="font-family: Courier New"> <span style="color: rgba(63, 127, 95, 1)">//1.</span></span><span style="font-family: 宋体">创建一个</span><span style="color: rgba(63, 127, 95, 1)"><span style="font-family: Courier New; text-decoration: underline">xml</span><span style="font-family: 宋体">解析器对象</span></span></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-family: Courier New"> <span style="color: rgba(255, 0, 0, 1)">SAXReader reader = <strong>new</strong> SAXReader(); </span></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><span style="font-family: Courier New"> <span style="color: rgba(63, 127, 95, 1)">//2.</span></span><span style="font-family: 宋体">读取</span><span style="color: rgba(63, 127, 95, 1)"><span style="font-family: Courier New; text-decoration: underline">xml</span><span style="font-family: 宋体">文档,返回</span><span style="font-family: Courier New">Document</span><span style="font-family: 宋体">对象</span></span></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-family: Courier New"> <span style="color: rgba(255, 0, 0, 1)">Document doc = reader.read(<strong>new</strong> File("./src/contact.xml"));</span></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-family: Courier New"> System.<span style="color: rgba(0, 0, 192, 1)"><em>out</em><span style="color: rgba(0, 0, 0, 1)">.println(doc);</span> </span></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-family: Courier New"> } <span style="color: rgba(127, 0, 85, 1)"><strong>catch</strong><span style="color: rgba(0, 0, 0, 1)"> (DocumentException e) {</span> </span></span></p>
<p><span style="font-family: Courier New"><span style="color: rgba(0, 0, 0, 1)"> e.<span style="background-color: rgba(192, 192, 192, 1)">printStackTrace</span>();</span> </span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-family: Courier New"> <span style="color: rgba(127, 0, 85, 1)"><strong>throw</strong><span style="color: rgba(0, 0, 0, 1)"> <span style="color: rgba(127, 0, 85, 1)"><strong>new</strong><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);</span> </span></span></span></span></p>
<p><span style="font-family: Courier New"><span style="color: rgba(0, 0, 0, 1)"> }</span></span><span style="font-family: Courier New"><span> </span></span></p>
<p><span style="color: rgba(0, 0, 0, 1); font-family: Courier New"> }</span></p>
</td>
</tr>
</tbody>
</table>
<h2>4.6 Domj4读取xml文件</h2>
<p> 节点:</p>
<p> Iterator Element.nodeIterator(); //获取当前标签节点下的所有子节点</p>
<p> 标签:</p>
<p> Element Document.getRootElement(); //获取xml文档的根标签 </p>
<p> Element ELement.element("标签名") //指定名称的第一个子标签</p>
<p> Iterator<Element> Element.elementIterator("标签名");// 指定名称的所有子标签</p>
<p> List<Element> Element.elements(); //获取所有子标签</p>
<p> 属性:</p>
<p> String Element.attributeValue("属性名") //获取指定名称的属性值</p>
<p> Attribute Element.attribute("属性名");//获取指定名称的属性对象 </p>
<p> Attribute.getName() //获取属性名称</p>
<p> Attibute.getValue() //获取属性值</p>
<p> List<Attribute> Element.attributes(); //获取所有属性对象</p>
<p> Iterator<Attribute> Element.attibuteIterator(); //获取所有属性对象</p>
<p> 文本:</p>
<p> Element.getText(); //获取当前标签的文本</p>
<p> Element.elementText("标签名") //获取当前标签的指定名称的子标签的文本内容</p>
<h1>5、<span style="color: rgba(0, 0, 0, 1)"><strong>总结: </strong></span></h1>
<p style="margin-left: 60px">本文主要介绍了一下:XML的作用、XML语法以及XML解析(DOM解析),下一篇将继续学习 xPath技术、 SAX解析 、 XML约束等xml操作知识。</p>
<p style="margin-left: 60px">参考文档:http://www.w3school.com.cn/xml/xml_syntax.asp</p>
</div>
<!--conend-->
<div class="p-2"></div>
<div class="arcinfo my-3 fs-7 text-center">
<a href='/t/etagid1788-0.html' class='tagbtn' target='_blank'>XML</a>
</div>
<div class="p-2"></div>
</div>
<div class="p-2"></div>
<!--xg-->
<div class="lbox p-4 shadow-sm rounded-3">
<div class="boxtitle"><h2 class="fs-4">相关</h2></div>
<hr>
<div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-564017.html">SpringMVC、SpringMVC XML配置(纯XML方式)</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-561826.html">SpringBoot+Mybatis加载Mapper.xml文件的两种方式</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-559489.html">3、maven的settings.xml文件</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-557446.html">Unity3D_08_XML文件创建,读取,修改,添加</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-554276.html">JSON 与 XML 的学习</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-552454.html">SAP导出报表格式XXl、XML、XLSX、MHTML选择</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-549780.html">Spring Boot 2.x基础教程:使用MyBatis的XML配置方式</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-549093.html">xml</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-548653.html">SpringBoot是如何做到无XML文件做配置的?</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-541245.html">IntelliJ IDEA 打开Maven项目,文件图标显示J/C及pom.xml文件图标问题</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-535471.html">C#(1):XML DOM、System.Xml.XmlDocument</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div><div class="row g-0 py-2 border-bottom align-items-center">
<div class="col-7 col-lg-11 border-lg-end">
<h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-535001.html">OpCenter xml client JaveScript库</a></h3>
<div class="ltag fs-8 d-none d-lg-block">
</div>
</div>
</div>
<!---->
<!---->
</div>
<!--xgend-->
</div>
<div class="col-lg-3 col-12 p-0 ps-lg-2">
<!--box-->
<!--boxend-->
<!--<div class="p-2"></div>-->
<!--box-->
<div class="lbox p-4 shadow-sm rounded-3">
<div class="boxtitle pb-2"><h2 class="fs-4"><a href="#">标签</a></h2></div>
<div class="clearfix"></div>
<ul class="m-0 p-0 fs-7 r-tag">
</ul>
<div class="clearfix"></div>
</div>
<!--box end-->
</div>
</div>
</div>
</main>
<div class="p-2"></div>
<footer>
<div class="container-fluid p-0 bg-black">
<div class="container p-0 fs-8">
<p class="text-center m-0 py-2 text-white-50">一品网 <a class="text-white-50" href="https://beian.miit.gov.cn/" target="_blank">冀ICP备14022925号-6</a></p>
</div>
</div>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?6e3dd49b5f14d985cc4c6bdb9248f52b";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</footer>
<script src="/skin/bootstrap.bundle.js"></script>
</body>
</html>