1 xml
可扩展标记语言,首先是一种语言
2 作用
- 数据交互
- 配置应用
- AJAX的基石
3 xml的元素
- <元素名 属性名=“属性值”>元素内容</元素名>
- 正常情况下,有元素内容,那么元素除了开始,还要有闭合
- 但是没有元素内容的情况下,可以写为自闭合标签形式<元素名 属性=“属性值/”>
- 所有xml标签元素都必须有结束标签
- xml标签对大小写敏感
- xml必须正确的嵌套
- 同级标签以缩进对齐
- 元素名称可以包含字母,数字或者其他的字符
- 元素名称不能以数字或标点符号开始
- 元素名称中不能含空格
- <![CDATA[]]>:只能用于文本
- 在编辑xml的时候,完全可以遵循面向对象的基本思路
4 xml的基本结构
4.1 节点
在xml当中,所有的内容,都是节点
4.2 文档节点
- xml文件本身,就是文档节点
- 名称:Document
- 类型:9
- 值:无
4.3 元素节点
- 标签
- 名称:element
- 类型:1
- 值:无
4.4 属性节点
- 属性定义在[标签/元素] 当中
- 名称:attribute
- 类型:2
- 值:有值
4.5 文本节点
- 在元素当中
- 名称
- 类型
- 值
4.6 注释节点
4.6.1 实例1
demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 根节点 -->
<clothesList>
<!-- 这样写没问题,但是,不方便解析 -->
<clothes id="1" type="s" height="<165">
</clothes>
<clothes id="2" type="s" maxHeight="165" minHeight="">
</clothes>
<clothes id="3" type="s">
<height maxHeight="165" minHeight=""/>
</clothes>
<clothes id="4" type="S">
<height name="max" minHeight="165"/>
<height name="min" minHeight=""/>
<explain>
<![CDATA[我爱你中国1]]>
</explain>
</clothes>
<clothes id="5" type="M">
<height name="max" minHeight="170"/>
<height name="min" minHeight="165"/>
<explain>
<![CDATA[我爱你中国2]]>
</explain>
</clothes>
<clothes id="6" type="L">
<height name="max" minHeight="175"/>
<height name="min" minHeight="170"/>
<explain>
<![CDATA[我爱你中国3]]>
</explain>
</clothes>
<clothes id="7" type="XL">
<height name="max" minHeight="180"/>
<height name="min" minHeight="175"/>
<explain>
<![CDATA[我爱你中国4]]>
</explain>
</clothes>
<clothes id="8" type="XXL">
<height name="max" minHeight="185"/>
<height name="min" minHeight="180"/>
<explain>
<![CDATA[我爱你中国5]]>
</explain>
</clothes>
</clothesList>
4.6.2 实例2
import java.io.File;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Test {
public static void main(String[] args) {
String path = Test.class.getClassLoader().getResource("demo1.xml").getPath();
// System.out.println(path);//获取文件路径
//读XML
//获取SAXReader解析器
SAXReader sax = new SAXReader();
//读取文件
try {
Document document = sax.read(new File(path));
// System.out.println(document.getNodeTypeName());
// System.out.println(document.getNodeType());
Element root = document.getRootElement();
// System.out.println(root.getNodeTypeName());
// System.out.println(root.getNodeType());
// Element clo1 = root.element("clothes");//获取指定名称的元素,如果有重名,则获取第一个
// List<Element> clos = root.elements();//获取所有的子元素,不包括孙子,只是儿子
List<Element> clos = root.elements("clothes");//获取所有指定名称的子元素
for(Element clo : clos){
// Attribute idAtt = clo.attribute("id");
// System.out.println(idAtt.getNodeTypeName());
// System.out.println(idAtt.getNodeType());
// System.out.println(idAtt.getStringValue());
// Attribute typeAtt = clo.attribute("type");
String id = clo.attributeValue("id");
String type = clo.attributeValue("type");
System.out.println("id:"+id + ",type:"+type);
List<Element> hs = clo.elements("height");
for(Element h : hs){
System.out.println("name:"+h.attributeValue("name")+",value:"+h.attributeValue("value"));
}
Element exp = clo.element("explain");
// System.out.println(exp.getText());
// System.out.println(exp.getTextTrim());
System.out.println(clo.elementTextTrim("explain"));;
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
4.6.3 实例3
package com.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Test2 {
public static void main(String[] args) throws Exception {
//覆盖
String path = Test2.class.getClassLoader().getResource("").getPath();
File f = new File(path,"demo2.xml");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Document document = DocumentHelper.createDocument();
Element e = DocumentHelper.createElement("ele");//根节点
document.add(e);
Element e1 = DocumentHelper.createElement("ele1");
e.add(e1);
Attribute att1 = DocumentHelper.createAttribute(e1, "id", "1");
e1.add(att1);
e1.addCDATA("test1");
Element e2 = DocumentHelper.createElement("ele2");
e.add(e2);
Attribute att2 = DocumentHelper.createAttribute(e2, "id", "2");
e2.add(att2);
e2.addCDATA("test2");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(f), format);
writer.write(document);
writer.close();
}
}
4.6.4 实例4
package com.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Test3 {
public static void main(String[] args) throws Exception {
//改--->先读再改
String path = Test3.class.getClassLoader().getResource("demo2.xml").getPath();
File f = new File(path);
SAXReader r = new SAXReader();
Document d = r.read(f);
Element root = d.getRootElement();
List<Element> eles = root.elements();
Element e3 = DocumentHelper.createElement("ele3");
Attribute att1 = DocumentHelper.createAttribute(e3, "id", "3");
e3.add(att1);
e3.addCDATA("test3");
eles.add(e3);//添加到原本的集合当中去
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(f), format);
writer.write(d);
writer.close();
}
}
5 注意事项
项目启动后,第一件事情,是将XML当中的数据读到集合当中,集合就相当于数据库的二级缓存,单反修改二级缓存中的数据,都会触发对XML的写操作。