XML(EXtensible Markup Language) 可扩展标记语言,它的作用是传输和保存数据,可以自定义标签。
1、内部DTD,在XML文档内,只对当前XML有效。
(1)DTD(文档类型定义),对XML文件的格式进行定义。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE animals[
<!ELEMENT animals (animal+)>
<!ELEMENT animal (name,color,favorite)>
<!ELEMENT animal id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>//不能再加其他标签
<!ELEMENT color (#PCDATA)>
<!ELEMENT faxorite (#PCDATA)>
]>
<animals>
<animal id="1">
<name>rabbit</name>
<color>white</color>
<favorite>胡萝卜</favorite>
</animal>
<animal id="2">
<name>sheep</name>
<color>black</color>
<favoriate>青草</favoriate>
</animal>
</animals>
(2)知识点总结:
<?xml version="1.0" encoding="UTF-8"?>
指明了版本号和字符集。
<name>rabbit</name>
为一个元素。
id=“1”为一个属性,属性必须用双引号。
根元素为animals,意味着此元素不能被其他元素包围,有且仅有一个。
常见的字符:
? : 只能出现一次
* :任意次
+ : 一次或多次
(3)属性的声明:
CDATA:字符数据
#REQUIRED:必须出现
#IMPLIED:不是必须出现
2、外部DTD
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT animals (animal+)>
<!ELEMENT animal (name,color,favorite)>
<!ELEMENT animal id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT color (#PCDATA)>
<!ELEMENT faxorite (#PCDATA)>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE animals STSTEM "Animal.dtd">
<animals>
<animal id="1">
<name>rabbit</name>
<color>white</color>
<favorite>胡萝卜</favorite>
</animal>
<animal id="2">
<name>sheep</name>
<color>black</color>
<favoriate>青草</favoriate>
</animal>
</animals>
3、XML的解析:
(1)获取元素:
package pers.xml.dtd;
import java.io.File;
import java.util.Iterator;
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) throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("src/Animals.xml"));
Element root = doc.getRootElement();
System.out.println("根元素"+root.getName());
Iterator<?> it = root.elementIterator();
while (it.hasNext()) {
Element e = (Element) it.next();
System.out.println( e.getName());
org.dom4j.Attribute id = e.attribute("id");
System.out.println(id.getValue());
Element name = e.element("name");
Element color = e.element("color");
Element favorite = e.element("favorite");
System.out.println(name.getName() + " " + name.getStringValue());
System.out
.println(color.getName() + " " + color.getStringValue());
System.out.println(favorite.getName() + " "+ favorite.getStringValue());
System.out.println("----------------------------------------------------");
}
}
}
被读取的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<animals>
<animal id="1">
<name>rabbit</name>
<color>white</color>
<favorite>胡萝卜</favorite>
</animal>
<animal id="2">
<name>sheep</name>
<color>black</color>
<favorite>青草</favorite>
</animal>
</animals>
文件里面的对于dtd的引用应删除:
<!DOCTYPE animals STSTEM "Animal.dtd">
(2)增加元素:
package pers.xml.dtd;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class Test1 {
public static void main(String[] args) throws IOException {
Document doc=DocumentHelper.createDocument();
Element root = doc.addElement("Animals");
Element Animal = root.addElement("Animal");
Animal.addAttribute("id", "A1");
Element name=Animal.addElement("name");
Element color = Animal.addElement("color");
Element favorite = Animal.addElement("favorite");
name.addText("骆驼");
color.addText("白");
favorite.addText("沙漠");
Writer writer= new FileWriter(new File("src/Animals1.xml"));
doc.write(writer);
writer.close();
}
}
程序运行后增加的文件:
<?xml version="1.0" encoding="UTF-8"?>
<Animals>
<Animal id="A1">
<name>骆驼</name>
<color>白</color>
<favorite>沙漠</favorite>
</Animal>
</Animals>