javawebday10(xpath查询节点 dom4j crud)

/*
 * 使用dom4j实现修改节点的操作
 *  修改第一个head下面的age元素的值<age>30</age>
 *      1、得到document
 *      2、得到根节点 然后得到第一个head元素
 *      3、得到第一个head下面的age
 *          element("")方法
 *      4、修改值30
 *          使用setText("文本内容")方法
 *      5、回写xml
 * 使用dom4j实现删除节点的操作
 *      删除第一个head下面的titile元素    
 * 
 *          1、得到document
 *          2、得到根节点
 *          3、得到第一个head标签
 *          4、得到第一个head下面的title元素
 *          5、删除(使用head删除title)
 *              得到school的父节点
 *                  第一种直接得到head
 *                  使用方法 getParent方法得到
 *              删除操作
 *                  在head上面执行remove方法删除节点
 *          6、回写xml
 * 使用dom4j获取属性的操作
 *  获取第一个head里面的属性id的值
 *  步骤
 *      1、得到document
 *      2、得到根节点
 *      3、得到第一个head元素
 *      4、得到head里面的属性值
 *          attributeValue("id");
 *          在head上面执行这个方法 里面的参数是属性名称
 *      
 * 使用dom4j支持xpath的操作
 *  可以直接获取到某个元素
 *  第一种形式
 *      /aa/bb/cc 表示一层一层的 aa下面 bb下面的cc
 *  第二种形式
 *      //bbb 表示和这个名称相同表示只要名称是bbb都得到
 *  第三种形式
 *      /* 所有元素
 *  第四种形式
 *      bbb[1] 表示第一个bbb元素
 *      bbb[last()] 表示最后一个bbb元素
 *  第五种形式
 *      //bb[@id] 表示只要bb元素上面有id属性 都得到
 *  第六种形式
 *      //bbb[@id='b1']  表示元素名称是bbb 在bbb上面有id属性 并且id属性是b1   
 * 使用dom4j支持xpath具体操作
 *  默认的情况下 dom4j不支持xpath
 *  如果想要在dom4j里面是有xpath
 *      第一步需要 引入支持xpath的jar包 使用 jaxen-1.1.6.jar
 *      需要把jar包导入文件
 *  在dom4j里面提供了两个方法 用来支持xpath
 *      selectNodes("xpath表达式")
 *          获取多个节点
 *      selectSingleNode("xpath表达式")
 *          获取一个节点
 *      使用xpath实现 查询xml所有name元素的值
 *          所有name元素的xpath表示
 *          使用 selectNodes("//name")
 *      1、得到document
        2、直接使用selectNodes("//name") 方法得到所有的name

            //得到document
            Document document = domUtils.document();
            //使用selecNodes()
            List<Node> list = document.selectNodes("//title");
            //遍历list集合
            for (Node node : list) {
                //node是每一个name元素
                //得到name元素里面的值
                String s = node.getText();
                System.out.println(s);
 *使用xpath实现 获取第一个head下面的title的值
 *  //head[@id="1"]/title
 *  使用到selectSingleNode("//head[@id="1"]/title")    
 *  步骤和代码
 *  
      1、得到document
      2、直接使用selectSingleNode 方法实现
        xpath : //head[@id="1"]/title

    //得到document
    Document document = domUtils.document();
    //直接使用selectSingleNode 方法实现  双引号里面用单引号来表示属性值
    Node name = document.selectSingleNode("//head[@id='1']/title");
    //得到name里面的值
    String s = name.getText();
    System.out.println(s);
 */
    //删除 根据id删除
    public static void delHead(String id) throws Exception {
         /*
         * 1、创建解析器
         * 2、得到document
         * 3、获取到所有的id
         *  使用xpath  //id 返回list集合
         * 4、遍历list集合
         * 5、判断集合里面的id和传递的id 是否相同
         * 6、如果相同 把id所在的head删除
         * 
         */
         //创建解析器
        SAXReader saxr = new SAXReader();
        // 得到document
        Document document = saxr.read("src/ee/11.xml");
        //获取所有的id标签 不是属性 xpath://id
        List<Node> list = document.selectNodes("//id");
        //遍历list集合
        for (Node node : list) {//node是每一个id元素
            //得到id的值
            String id1 = node.getText();
            //判断id和传递的id是否相同
            if(id1.equals(id)) {//id相同
                //得到head节点
                Element head = node.getParent();
                //获取head的父节点
                Element rest = head.getParent();
                //删除head
                rest.remove(head);
            }
        }
        //回写xml
        OutputFormat of = OutputFormat.createPrettyPrint();
        XMLWriter xmlw = new XMLWriter(new FileOutputStream("src/ee/11.xml"),of);
        xmlw.write(document);
        xmlw.close();
    }
    //使用sax 添加标签
    public static void addAbc() throws Exception{
        /*
         * 1、创建解析器
         * 2、得到document
         * 3、获取到根节点
         * 4、在根节点上面创建abc标签
         * 5、在ab标签上面一次添加  a b c 
         * 6、在a b c 上面依次添加值
         * 7、回写xml
         */
        //创建解析器
        SAXReader saxr = new SAXReader();
        //得到document
        Document document = saxr.read("src/ee/11.xml");
        //得到根节点
        Element root = document.getRootElement();
        //在根节点上面添加abc标签
        Element abc = root.addElement("abc");
        //在abc标签上面添加 a b c标签
        Element a = abc.addElement("a");
        Element b = abc.addElement("b");
        Element c = abc.addElement("c");
        //a b c 上面一次添加值
        a.setText("a1");
        b.setText("b1");
        c.setText("c1");
        //回写xml
        OutputFormat of = OutputFormat.createPrettyPrint();
        XMLWriter xmlw = new XMLWriter(new FileOutputStream("src/ee/11.xml"),of);
        xmlw.write(document);
        xmlw.close();
    }
    //使用xpath实现 获取第一个head下面的title的值
    private static void test2() throws Exception {
        /*
         * 1、得到document
         * 2、直接使用selectSingleNode 方法实现
         *  xpath : //head[@id="1"]/title
         */
        //得到document
        Document document = domUtils.document();
        //直接使用selectSingleNode 方法实现  双引号里面用单引号来表示属性值
        Node name = document.selectSingleNode("//head[@id='1']/title");
        //得到name里面的值
        String s = name.getText();
        System.out.println(s);
    }
    //查询xml中所有name元素的值
    public static void test1() throws Exception{
        /*
         * 1、得到document
         * 2、直接使用selectNodes("//name") 方法得到所有的name
         */
        //得到document
        Document document = domUtils.document();
        //使用selecNodes()
        List<Node> list = document.selectNodes("//title");
        //遍历list集合
        for (Node node : list) {
            //node是每一个name元素
            //得到name元素里面的值
            String s = node.getText();
            System.out.println(s);
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值