xml语法
- XML的文档声明
<?xml version="1.0" encoding="utf-8"?>
文档声明必须写在第一行第一列
属性:
version:xml的版本 1.0(使用) 1.1
encoding:xml的编码 utf-8 gbk iso-8859-1(不包含中文)
standalone:是否需要依赖其他的文件 yes/no
- 定义元素(标签)
标签定义有开始必须有结束
包含标签主体:
<person>文本内容</person>
标签没有内容:
<person />
一个XML文档必须有且仅有一个根标签,其他标签都是根标签的子标签
XML代码区分大小写,不能以数字和下划线(_)开头
不能以xml、XML、Xml等开头
不能包含空格
名称中间不能包含冒号(😃
- 定义属性
<person id="1">文本内容</person>
属性为id
- 注释
<!---->
注释不能放在第一行第一列
- 特殊字符
特殊字符可以通过实体符号表示
& &
< <
> >
" "
' '
- CDATA区
可以解决多个字符都需要转义的操作
<![CDATA[内容]]>
<![CDATA[if(a > b && b < c) {}]]>
- PI指令(处理指令)
处理指令,简称PI(Processing Instruction)
用来指挥软件如何解析XML文档
语法必须以"<?"作为开头,以"?>"作为结尾
常用处理指令:
XML声明:<?xml version="1.0" encoding="utf-8"?>
xml-stylesheet指令:指示XML文档所使用的CSS样式XSL
<?xml-stylesheet type="text/css" href="style.css"?>
注:对中文命名的标签元素不起作用
- XML的语法总结
所有XML元素都必须有关闭标签
XML标签对大小写敏感
XML必须正确的嵌套顺序
XML文档必须有且只有一个根元素
XML的属性值需要加引号(单、双)
特殊字符必须转义 – <![CDATA[内容]]>
XML中的空格、回车换行会在解析时被保留
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!--以上标签是xml的声明-->
<!--version 表示xml版本,从诞生就没更新过-->
<!--encoding 表示xml本身的编码方式-->
<books><!--表示多个图书信息-->
<book sn="01"><!--book表示一个图书信息 sn属性表示图书序列号-->
<name>时间简史</name>
<author>霍金</author>
<price>666</price>
</book>
<book sn="02">
<name>时间捡屎</name>
<author>
<!-- 这里面的内容不会被解析只是文本情况存在,因此可以显示特殊字符-->
<![CDATA[可以显示特殊字符而不使用转义字符]]>
<![CDATA[<<<管文赫>>>]]>
</author>
<price>666</price>
</book>
<!-- 可以转化为单标签-->
<!-- 属性值必须用引号引起来-->
<!-- <book sn="04" name="空间简史" author="忘记是谁" price="888"/>-->
</books>
提取xml信息类
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import java.util.List;
public class Dom4jTest {
@Test
public void test1() throws DocumentException {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read("src/books.xml");
System.out.println(document);
} catch (Exception e) {
e.printStackTrace();
}
}
// 读取xml文件生成book类
@Test
public void test2() throws DocumentException {
// 读取books.xml文件
SAXReader reader = new SAXReader();
// 在junit测试中,相对路径是从模块名开始算的
Document document = reader.read("src/books.xml");
// 通过Document对象获取根元素
Element rootElement = document.getRootElement();
// System.out.println(rootElement);
List<Element> books = rootElement.elements("book");
// 遍历
for (Element book : books) {
// System.out.println(book.asXML());
Element nameElement = book.element("name");
System.out.println(nameElement.asXML());
String name = nameElement.getText();
System.out.println(name);
String price = book.elementText("price");
System.out.println(price);
String author = book.elementText("author");
System.out.println(author);
String snValue = book.attributeValue("sn");
// 将取出的元素进行封装
System.out.println(new book(snValue, name, Double.parseDouble(price), author));
}
}
}
封装类
import java.math.BigDecimal;
public class book {
private String sn;
private String name;
private double prices;
private String author;
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrices() {
return prices;
}
public void setPrices(double prices) {
this.prices = prices;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public book(String sn, String name, double prices, String author) {
this.sn = sn;
this.name = name;
this.prices = prices;
this.author = author;
}
public book() {
}
@java.lang.Override
public java.lang.String toString() {
return "book{" +
"sn='" + sn + '\'' +
", name='" + name + '\'' +
", prices=" + prices +
", author='" + author + '\'' +
'}';
}
}
实用性
项目中xml一般要和约束搭配使用,xml提供格式,约束提供标签对应的语法。
web-app标签中就是提供了web.xml对应的语法约束。
否则单纯的xml语法,只要是标签格式就可以,如同上面的例子。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置监听初始时加载的spring文件名称-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:aplicationContext.xml</param-value>
</context-param>
<!-- struts2的配置-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring监听器的配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>