美文网首页
java笔记: Dom4j的使用Sax篇

java笔记: Dom4j的使用Sax篇

作者: 梦之志 | 来源:发表于2018-09-13 22:25 被阅读0次
        //获取第一个p1里面的属性id1的值
        public static void GetValue()throws Exception{
            //1.创建解析器
            SAXReader saxReader = new SAXReader();
    
            //2.得到document
            Document document = saxReader.read(PATH);
    
            //3.得到根目录
            Element rootElement = document.getRootElement();
    
            //4.得到p1标签
            Element p1 = rootElement.element("p1");
    
            //5.得到属性id1的值
            String id1 = p1.attributeValue("id");
    
            System.out.println("id:" + id1);
    
        }
    
        //删除某个元素p1标签下的age标签
        public static void DeleteAge() throws Exception{
    
            //1.创建解析器
            SAXReader saxReader = new SAXReader();
    
            //2.得到Document
            Document document = saxReader.read(PATH);
    
            //3.得到根目录
            Element root = document.getRootElement();
    
            //4.得到P1标签
            Element p1 = root.element("p1");
    
            //5. 得到age标签
            Element age = p1.element("age");
    
            //6.删除age标签
            p1.remove(age);
    
            //7. 回写XML
            OutputFormat format = OutputFormat.createCompactFormat();
    
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(PATH),format);
    
            xmlWriter.write(document);
    
            xmlWriter.close();
    
        }
    
        //修改第一个p1下面的name元素的值
        public static void ChangeName() throws Exception{
    
            //1.创建解析器
            SAXReader saxReader = new SAXReader();
    
            //2.得到Document
            Document document = saxReader.read(PATH);
    
            //3.得到根ELEMENT
            Element root = document.getRootElement();
    
            //4.得到p1
            Element p1 = root.element("p1");
    
            //5. 得到 p1标签下的name标签
            Element name = p1.element("name");
    
            //6.更改标签的值
            //name.attributeValue("WangWu"); //错误的修改方式
            name.setText("WangWu");
    
            //7.回写XML
            OutputFormat outputFormat = new OutputFormat();
    
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(PATH),outputFormat);
    
            xmlWriter.write(document);
    
            xmlWriter.close();
    
        }
    
        //在第一个p1下面的age标签之前添加<School>duchangerzhong</School>
        public static void AddSchool() throws Exception{
    
            //1. 创建解析器
            SAXReader saxReader = new SAXReader();
    
            //2.创建Document
            Document document = saxReader.read(PATH);
    
            //3.获取根Element
            Element root = document.getRootElement();
    
            //4.获取p1下所有的子元素
            Element p1 = root.element("p1");
    
            //5.获取p1下所有的子元素
            List list = p1.elements();
    
            //6.创建School元素
            Element school = DocumentHelper.createElement("school");
    
            school.setText("duchangerzhong");
    
            //7.在指定位置插入元素
            list.add(1,school);
    
            //list.add(list.size() - 1,school); //在末尾添加元素
    
            //回写XML
            OutputFormat outputFormat = new OutputFormat();
    
            XMLWriter writer = new XMLWriter(new FileOutputStream(PATH),outputFormat);
    
            writer.write(document);
    
            writer.close();
    
    
        }
    
        //在第一个p1标签末尾添加一个元素 <sex>nv</sex>
        public static void AddLastSex() throws Exception{
    
            //1. 创建解析器
            SAXReader saxReader = new SAXReader();
    
            //2. 创建Document
            Document document = saxReader.read(PATH);
    
            //3.获取根目录
            Element root = document.getRootElement();
    
            //4. 获取p1标签
            Element p1 = root.element("p1");
    
            //5.在p1标签内的末尾添加标签
            Element sex = p1.addElement("Sex");
    
            //6.给sex标签添加内容
            sex.setText("nv");
    
            //7.回写XML
            OutputFormat outputFormat = new OutputFormat();
    
            XMLWriter writer = new XMLWriter(new FileOutputStream(PATH),outputFormat);
    
            writer.write(document);
    
            writer.close();
    
        }
    
        //获取第二个name元素里面的值
        public static void GetSecondValue() throws Exception{
            //1.创建解析器
            SAXReader saxReader = new SAXReader();
    
            //2.获取Document
            Document document = saxReader.read(PATH);
    
            //3.获取根目录
            Element root = document.getRootElement();
    
            //4.获取根目录下所有的子元素
            List list1 = root.elements();
    
            //5.获取指定元素
            Element p1 = (Element) list1.get(1);
    
            //6.获取第二个p1下的name元素
            Element name = p1.element("name");
    
            String s = name.getText(); //获取标签中的值
    
            System.out.println("name标签中的值: " + s);
    
    
        }
    
        //查询xml中所有name元素的值
        public static void AllNameValue() throws Exception{
    
            //1.创建构造器
            SAXReader saxReader = new SAXReader();
    
            //获取Document
            Document document = saxReader.read(PATH);
    
            //获取根目录
            Element root = document.getRootElement();
    
            //获取根目录下所有的元素
            List<Element> list = root.elements();
    
            //遍历数据
            for (Element element:list){
    
                if (element.element("name") != null){
                    Element name = element.element("name");
    
                    System.out.println( name.getText());
                }else {
                    System.out.println("本元素没有name子元素!");
                }
            }
    
        }
    

    相关文章

      网友评论

          本文标题:java笔记: Dom4j的使用Sax篇

          本文链接:https://www.haomeiwen.com/subject/ykaggftx.html