1、xml : extensible markup language 可扩展标记语言 version=1.0
可扩展:所有的标签都是自定义的
功能:数据存储
配置文件
数据传输
html 与 xml 区别
html语法松散,xml语法严格
html做页面展示,xml做数据存储
html所有标签都是预定义的,xml所有标签都是自定义的
2、xml语法:
文档声明
version 版本号 固定值1.0
encoding 指定文档的码表 默认值为iso-8859-1
standalone 指定文档是否独立 yes 或 no
必须写在xml文档的第一行
写法:8d78aa6aa01dc9bf77de63074feb2293
属性
元素 xml文档中的标签
元素名称区分大小写
数字不能开头
文档中必须有且只能有一个根元素
元素需要正确闭合 6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956 076402276aae5dbec7f672f8f4e5cc81
元素需要正确嵌套
元素名称要遵守
文本
70cec81b2be067f3c87df6a249b1ec9e
转义字符 >;
cdata 里边的数据会原样显示
属性
属性值必须用引号引起来,单双引号都行
注释
19791207a4e0a22c816ac020ecd8cae0
处理指令:现在基本不用
ecb56876e6a19d3cc2d0b356114cfd2b
3、xml约束
约束就是xml的书写规则
约束的分类:
导入xsd约束文档
编写根标签
引入实例名称空间 xmlns:xsi=www.w3.org/2001/xmlschema-instance
引入名称空间 xsi:schemalocation=www.itcast.cn/xml student.xsd
引入默认的名称空间
student.xsd
student.xml
<?xml version="1.0"?>
<xsd:schema xmlns="www.itheima.cn/xml"
xmlns:xsd="www.w3.org/2001/xmlschema"
targetnamespace="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>
<?xml version="1.0" encoding="utf-8" ?>
<!--
1、编写根标签
2、引入实例名称空间 xmlns:xsi="www.w3.org/2001/xmlschema-instance"
3、引入名称空间 xsi:schemalocation="www.itcast.cn/xml student.xsd"
4、引入默认的名称空间
-->
<students
xmlns="www.itheima.cn/xml"
xsi:schemalocation="www.itheima.cn/xml student.xsd"
xmlns:xsi="www.w3.org/2001/xmlschema-instance"
>
<student number="itheima_1001">
<name>asfd</name>
<age>12</age>
<sex>male</sex>
</student>
</students>
<students
xmlns:itheima="www.itheima.cn/xml"
xsi:schemalocation="www.itheima.cn/xml student.xsd"
xmlns:xsi="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>
内部dtd 在xml内部定义dtd
外部dtd 在外部文件中定义dtd
student.dtd
student.xml
本地dtd文件 <!doctype students system "student.dtd">
网络dtd文件 <!doctype students public "名称空间" “student.dtd”>
<!element students (student*) >
<!element student (name,age,sex)>
<!element name (#pcdata)>
<!element age (#pcdata)>
<!element sex (#pcdata)>
<!attlist student number id #required> 唯一的,必须的
<?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>
dtd 约束不严谨
schema
4、xml解析
解析xml可以做:
如果xml作为配置文件:读取
如果xml作为传输文件:写、读
xml解析思想:
优点:不占内存,速度快
缺点:只能读取,不能回写
优点:因为在内存中会形成dom树,可以对dom树进行增删改查
缺点:dom树非常占内存,解析速度慢
document element text attribute comment
dom:将文档加载到内存,形成一棵dom树(document对象),将文档的各个组成部分封装为一些对象
sax:逐行读取,基于事件驱动
xml常用的解析器
定义了一种规则
使用方法
使用步骤
xpath:
public classtestxpath2 {
@test
publicvoidtest()throwsexception{
saxreaderread= new saxreader();
documentdocument= read.read("src/dom4jtest.xml");
listnodes= document.selectnodes("/bookstore//book/title");
for(inti= 0;i< nodes.size();i++) {
nodenode= (node)nodes.get(i);
system.out.println(node.gettext());
}
}
}
selectsinglenode()
selectnodes()
注意:要导包 jaxen...jar
创建解析器 saxreader reader = new saxreader()
解析xml 获得document对象 document document = reader.read(url)
// nodename 选取此节点。
// / 从根节点选取。
// // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
// .. 选取当前节点的父节点。
// @ 选取属性。
// [@属性名] 属性过滤
// [标签名] 子元素过滤
@test
//遍历所有元素节点
publicvoidtest2()throwsexception{
//创建一个xml解析对象
saxreaderreader= new saxreader();
//把xml文档加载到document对象中
documentdocument= reader.read("src/book.xml");
elementroot= document.getrootelement();
treewalk(root);
}
privatevoidtreewalk(elementele){
//输出当前节点的名字
system.out.println(ele.getname());
//ele.nodecount()得到当前节点的所有子节点的数量
for(inti= 0;i<ele.nodecount();i++){
//取出下标为i的节点
nodenode= ele.node(i);
//判断当前节点是否为标签
if(nodeinstanceofelement){
//把node强转为标签(element)
treewalk((element)node);
}
}
}
}
public classtestdom4j {
@test
publicvoidtest1()throwsexception{
//创建一个xml解析对象
saxreaderreader= new saxreader();
//把xml文档加载到document对象中
documentdocument= reader.read("src/book.xml");
elementroot= document.getrootelement();
// element booknode = root.element("书");
// system.out.println(booknode.getname());
//得到当前节点所有的子节点
listlist= root.elements();
//得到第二本书对象
elementsecondbook= (element)list.get(1);
//得到当前节点的文本内容
stringname= secondbook.element("书名").gettext();
system.out.println(name);
}
导入jar包 dom4j.jar
创建解析器
解析xml 获得document对象
saxreader reader = new saxreader()
document document = reader.read(url)
jaxp sun公司提供的解析 支持dom和sax
jdom
dom4j dom for java民间方式,但是是事实方式,非常好,支持dom
解析xml
xpath 专门用于查询
以上就是带你深入了解xml的详细内容。
