<hr>
<employee no="3309">
<name>张三</name>
<age>31</age>
<salary>4000</salary>
<department>
<dname>会计部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="3310">
<name>李四</name>
<age>23</age>
<salary>34000</salary>
<department>
<dname>工程部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
</hr>
<!DOCTYPE hr SYSTEM "hr.dtd">
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT hr (employee+)>
<!ELEMENT employee (name,age,salary,department)>
<!ATTLIST employee no CDATA "">
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
<!ELEMENT department (dname,address)>
<!ELEMENT dname (#PCDATA)>
<!ELEMENT address (#PCDATA)>
记住加空格,否则失效
<hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hr.xsd">
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="hr">
<!-- complexType标签含义是复杂节点,包含子节点时必须使用这个标签 -->
<complexType>
<sequence>
<element name="employee" minOccurs="1" maxOccurs="999">
<complexType>
<sequence>
<element name="name" type="string"></element>
<element name="age">
<simpleType>
<restriction base="integer">
<minInclusive value="18"></minInclusive>
<maxInclusive value="60"></maxInclusive>
</restriction>
</simpleType>
</element>
<element name="salary" type="integer"></element>
<element name="department">
<complexType>
<sequence>
<element name="dname" type="string"></element>
<element name="address" type="string"></element>
</sequence>
</complexType>
</element>
</sequence>
<attribute name="no" type="string" use="required"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
Dom4j遍历XML
记得导入jar包
package com.imooc.dom4j;
import java.awt.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrReader {
public void readXml() {
String file="d:/eclipse/xml/src/hr.xml";
//SAXReader类是读取XML文件的核心类,用于将XML解析后以树的形式保存在内存中
SAXReader reader=new SAXReader();
try {
Document document=reader.read(file);
//获取XML文档的根节点,即hr标签
Element root=document.getRootElement();
//elements方法用于获取指定的标签集合
java.util.List<Element> employees = root.elements("employee");
for(Element employee:employees) {
//element方法用于获取唯一的子节点对象
Element name=employee.element("name");
//getText()方法用于获取标签文本
String empName=name.getText();
System.out.println(empName);
System.out.println(employee.elementText("age"));
System.out.println(employee.elementText("salary"));
Element department=employee.element("department");
System.out.println(department.element("dname").getText());
System.out.println(department.element("address").getText());
Attribute att=employee.attribute("no");
System.out.println(att.getText());
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
HrReader reader=new HrReader();
reader.readXml();
}
}
Dom4j更新XML(插入数据)
package com.imooc.dom4j;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrWriter {
public void writerXml() {
String file="d:/eclipse/xml/src/hr.xml";
SAXReader reader=new SAXReader();
try {
Document document=reader.read(file);
Element root=document.getRootElement();
Element employee=root.addElement("employee");
employee.addAttribute("no", "3331");
Element name =employee.addElement("name");
name.setText("李铁柱");
employee.addElement("age").setText("34");
employee.addElement("salary").setText("3600");
Element department=employee.addElement("department");
department.addElement("dname").setText("人事部");
department.addElement("address").setText("XX大厦-B105");
Writer writer=new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
document.write(writer);
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
HrWriter hrWriter=new HrWriter();
hrWriter.writerXml();
}
}
XPath表达式
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XPathTest {
public void xpath(String xpathExp){
String file="d:/eclipse/xml/src/hr.xml";
SAXReader reader=new SAXReader();
try {
Document document=reader.read(file);
//核心
List<Node> nodes=document.selectNodes(xpathExp);
for(Node node : nodes){
//node是Element和属性的父类,需要强制转换
Element emp=(Element)node;
System.out.println(emp.attributeValue("no"));
System.out.println(emp.elementText("name"));
System.out.println(emp.elementText("age"));
System.out.println(emp.elementText("salary"));
System.out.println("=========================");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
XPathTest testor=new XPathTest();
//testor.xpath("/hr/employee");
//testor.xpath("//employee");
//testor.xpath("//employee[salary<4000]");
//testor.xpath("//employee[name='李铁柱']");
//testor.xpath("//employee[@no=3309]");
//第一位出现的employee节点
//testor.xpath("//employee[1]");
//testor.xpath("//employee[last()]");
//testor.xpath("//employee[position()<3]");
testor.xpath("//employee[2]|//employee[1]");
}
}