zoukankan      html  css  js  c++  java
  • [转存]有关dom4j解析xml的各种操作的代码

    程序代码

    import java.util.*;
    import java.io.File;

    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;
    import org.dom4j.Node;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Attribute;

    public class Dom4jParseXml {
        //通过xml文件名得到DOM
        public Document getDocument(String xmlFileName) throws DocumentException {
            SAXReader reader = new SAXReader();
            Document d = reader.read(new File(xmlFileName));
            return d;
        }
        //重载,通过xml文件内容得到DOM
        public Document getDocument(String xmlContent, boolean b) throws
                DocumentException {
            Document d = DocumentHelper.parseText(xmlContent);
            return d;
        }

        //输出字符串
        public String transformDOM(Document d) {
            String xmlContent = "";
            xmlContent = d.asXML();
            return xmlContent;
        }

        //得到节点
        public Element getNode(Document d, String elePath, String eleValue) {
            Element ele = null;
            List l = d.selectNodes(elePath);
            Iterator iter = l.iterator();
            while (iter.hasNext()) {
                Element tmp = (Element) iter.next();
                if (tmp.getText().equals(eleValue)) {
                    ele = tmp;
                }
            }
            return ele;
        }
        //重载,得到节点
        public Element getNode(Document d, String eleName) {
            Element ele = (Element) d.selectSingleNode(eleName);
            return ele;
        }

        //增加节点
        public void addNode(Element parentEle, String eleName, String eleValue) {
            Element newEle = parentEle.addElement(eleName);
            newEle.setText(eleValue);
        }

        //增加属性节点
        public void addAttribute(Element ele, String attributeName,
                                 String attributeValue) {
            ele.addAttribute(attributeName, attributeValue);
        }

        //删除节点
        public void removeNode(Element parentEle, String eleName, String eleValue) {
            Iterator iter = parentEle.elementIterator();
            Element delEle = null;
            while (iter.hasNext()) {
                Element tmp = (Element) iter.next();
                if (tmp.getName().equals(eleName) && tmp.getText().equals(eleValue)) {
                    delEle = tmp;
                }
            }
            if (delEle != null) {
                parentEle.remove(delEle);
            }
        }

        //删除属性
        public void removeAttr(Element ele, String attributeName) {
            Attribute att = ele.attribute(attributeName);
            ele.remove(att);
        }

        //修改节点值
        public void setNodeText(Element ele, String newValue) {
            ele.setText(newValue);
        }

        //修改属性值
        public void se
  • 相关阅读:
    生产者消费者模型
    查看网络通不通的几种方法
    tomcat在45秒内没有启动,启动超时
    request获取各种路径
    修改web项目发布路径
    web.xml不同版本的头
    Web.xml 错误或异常页面配置
    ModelAndView command
    java初始化顺序
    初始化时的过程
  • 原文地址:https://www.cnblogs.com/anuoruibo/p/3108702.html
Copyright © 2011-2022 走看看