2020-04-01-Java操作xml文档

2020-04-01  本文已影响0人  ssss2

DOM树

捕获.PNG

DOM4j

将整个xml视为document对象
xml标签被视为element对象

Dom4j遍历xml文件

public class HrReader {
    public void readXml(){
        String file = "d:/workspace/xml/src/hr.xml";
        //SAXReader类是读取xml文件的核心类,用于将xml解析后以树的形式保存在内存中
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(file);
            //获取xml的根节点,即hr标签
            Element root = document.getRootElement();
            //elements用于获取指定的标签集合
            List<Element> employees =  root.elements("employee");
            for(Element employee : employees){
                //element方法用于获取唯一的子节点对象
                Element name = employee.element("name");
                String empName = name.getText();
                                //getTex()用于获取标签文本
                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文件

public class HrWriter {
    public void writeXml(){
        String file = "d:/workspace/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", "3311");
            //创建属性
            Element name = employee.addElement("name");
            name.setText("李铁柱");
            employee.addElement("age").setText("37");
            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.writeXml();
    }
}

XPath路径表达式

简化数据查询与提取的过程

基本表达式

捕获.PNG

谓语表达式

捕获.PNG
public class XPathTestor {
    public void xpath(String xpathExp){
        String file = "E:/lianxi/xml/hr.xml";
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(file);
            
            List<Node> nodes = document.selectNodes(xpathExp);
            for(Node node : nodes){
                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) {
        XPathTestor testor = new XPathTestor();
//      testor.xpath("/hr/employee");
//      testor.xpath("//employee");
//      testor.xpath("//employee[salary<4000]");
//      testor.xpath("//employee[name='李铁柱']");
//      testor.xpath("//employee[@no=3304]");
//      testor.xpath("//employee[1]");
//      testor.xpath("//employee[last()]");
        //testor.xpath("//employee[position()<3]");
        testor.xpath("//employee[3] | //employee[8]");
        
    }
}

上一篇下一篇

猜你喜欢

热点阅读