XML

2018-09-18  本文已影响0人  磊_5d71

XML与HTML的比较

<?xml version="1.0" encoding="UTF-8"?>
<!-- 人力资源管理系统 -->
<hr>
    <employee no="3309">
        <name>张三</name>
        <age>31</age>
        <salary>4000</salary>
        <department>
            <dname>会计部</dname>
            <address>隆安大厦-B103</address>
        </department>
    </employee>
    
    <employee no="3310">
        <name>李四</name>
        <age>23</age>
        <salary>3000</salary>
        <department>
            <dname>工程部</dname>
            <address>隆安大厦-B104</address>
        </department>
    </employee>
</hr>

XML的用途

XML文档结构

XML语义约束

DTD定义节点
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT hr (employee+)>
<!-- employee后面一定要加空格-->
<!ELEMENT employee (name,age,salary,department)>
<!-- employee属性的设置-->
<!ATTLIST employee no CDATA "">
<!-- 定义标签里面的内容为纯文本节点-->
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
<!ELEMENT department (dname,address)>
<!ELEMENT dname (#PCDATA)>
<!ELEMENT address (#PCDATA)>
XML Schema
<?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="9999">
                    <complexType>
                        <sequence>
                            <element name="name" type="string"></element>
                            <element name="age">
                                <simpleType>
                                    <restriction base="integer">
                                        <minExclusive value="18"></minExclusive>
                                        <maxExclusive value="60"></maxExclusive>
                                    </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>

DOM文档对象模型

Dom4j java库

package com.alan.dom4j;

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 HrReader {

    public void readXml() {
        
        File xmlFile = new File("/Users/alan/eclipse-workspace/XmlProj/src/hr.xml");

        // SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中
        SAXReader reader = new SAXReader();
        try {
            //将树状结构的XML赋值给Document对象,一个XML对应一个Document对象
            Document document = reader.read(xmlFile);
            //获取XML文档的根结点,即hr标签
            Element root = document.getRootElement();
            //elements方法用于获取指定的标签集合
            List<Element> employeesList = root.elements();
            for(Element employee:employeesList) {
                //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.elementText("dname"));
                System.out.println(department.elementText("address"));
                //或缺属性值
                Attribute atr =  employee.attribute("no");
                System.out.println(atr.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        HrReader hr = new HrReader();
        hr.readXml();
    }

}

Dom4j更新XML

package com.alan.dom4j;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 writeXml() {

        File xmlFile = new File("/Users/alan/eclipse-workspace/XmlProj/src/hr.xml");
        // SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中
        SAXReader reader = new SAXReader();
        try {
            // 在内存中构建DOM模型
            Document document = reader.read(xmlFile);
            Element root = document.getRootElement();
            Element employee = root.addElement("employee");
            employee.addAttribute("no", "3311");
            Element name = employee.addElement("name");
            name.setText("李铁柱");
            employee.addElement("age").setText("22");
            employee.addElement("salary").setText("3600");

            Element department = employee.addElement("department");
            Element dname = department.addElement("dname");
            dname.setText("人事部");
            Element address = department.addElement("address");
            address.setText("隆安大厦-B105");
            // 将内存中的模型写入到xml中
            try {
                Writer writer = new OutputStreamWriter(new FileOutputStream(xmlFile));
                document.write(writer);
                writer.flush();
                writer.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HrWriter hw = new HrWriter();
        hw.writeXml();
    }

}

XPath路径表达式

package com.alan.dom4j;

import java.io.File;
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 = "/Users/alan/eclipse-workspace/XmlProj/src/hr.xml";
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(file);
            List<Node> nodes = document.selectNodes(xpathExp);
            for(Node node:nodes) {
                Element element = (Element) node;
                System.out.println(element.attributeValue("no"));
                System.out.println(element.elementText("name"));
                System.out.println(element.elementText("age"));
                System.out.println(element.elementText("salary"));
                System.out.println("===========================");

            }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    public static void main(String[] args) {
        XPathTest xt = new XPathTest();
        //xt.xpath("/hr/employee");
        //xt.xpath("//employee");
        //查询工资小于3500的员工信息
        //xt.xpath("//employee[salary<=3600]");
        //xt.xpath("//employee[name='李铁柱']");
        //xt.xpath("//employee[@no=3309]");
        //xt.xpath("//employee[1]");
        //xt.xpath("//employee[last()]");
        //xt.xpath("//employee[position()<3]");
        xt.xpath("//employee[1] | //employee[3]");

    }

}
上一篇下一篇

猜你喜欢

热点阅读