xml dom4j解析

首先导包:
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
代码

    /**
         * Document doc = reader.read(new File("./src/bean-action.xml"));
         * Element ele = doc.getRootElement();得到根标签
         * ele.element("bean");得到指定标签
         * e1e.attributeValue("id");得到指定属性
         * e1e.getText();得到文本
         * 
         * 
         */
public class Dom4jTest {
    static SAXReader reader = null;
    static Document doc = null;
    static Iterator<Node> it = null;
    static{
        //创建xml解析器对象
         reader = new SAXReader();
        //读取xml文档,返回Document
         try {
            doc = reader.read(new File("./src/bean-action.xml"));
            //nodeIterator 得到当前节点下的所有子节点对象(不包括孙以下的节点)
            it = doc.nodeIterator();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//      nodeTest( it );
//      Element el = doc.getRootElement();
//      getChildNodes(el);

//      Element ele = doc.getRootElement();
//      StringBuffer sb = new StringBuffer();
//      printXML(ele,sb);
//      System.out.println(sb.toString());


        test5();
    }
    /**
     * *
     * description:循环得到所有子节点
     * @param it
     * @throws Exception
     */
    public static void nodeTest( Iterator<Node> it ) throws Exception{  

                while(it.hasNext()){//判断是否有下一个元素
                    Node node = it.next();
//                  String name = node.getName();
//                  System.out.println(name);
                    //继续去除其下面的子节点,只有标签节点才有子节点,判断当前节点是否是标签节点
                    if(node instanceof Element){
                        Element elemnt = (Element)node;
                        System.out.println(elemnt.getName());//得到节点名称
                        Iterator<Node> it1 = elemnt.nodeIterator();
                        nodeTest(it1);
                    }                   
                }
    }
    /**
     * *
     * description:得到所有子节点
     * @param el
     */
    public static void getChildNodes( Element el ){
        System.out.println(el.getName());
        Iterator<Node> it = el.nodeIterator();
        while(it.hasNext()){
            Node node = it.next();
            if(node instanceof Element){
                Element e = (Element)node;
                getChildNodes(e);
            }
        }
    }

    public static void Test(){
        //读取文档
        //得到跟标签
        Element ele = doc.getRootElement();
        //得到当前标签下指定名称的第一个子标签
        Element e1 = ele.element("bean");
        System.out.println(e1.getName());
        //得到当前标签下指定名称的所有标签
        Iterator<Element> it = ele.elementIterator("bean");
        //得到当前标签下的所有子标签
        List<Element> list = ele.elements();
        //获取更深层次的标签(方法只能一层层的获取)
        Element e2 = doc.getRootElement().element("bean").element("property");
        System.out.println(e2.getName());

    }
    /**
     * 获取属性
     */
    public static void test1(){
        //获取属性:(先获取属性所在的标签对象,然后才能获取属性)
        //1.得到标签对象
        Element ele = doc.getRootElement();
        Element e1 = ele.element("bean");
        //2.得到属性
        //2.1得到指定名称的属性值
        String id = e1.attributeValue("id");
        System.out.println(id);
        //2.2得到指定属性名称的属性对象
        Attribute attribute = e1.attribute("id");
        System.out.println(attribute.getName()+":::"+attribute.getValue());
        //2.3得到所有属性对象 返回list集合
        List<Attribute> list = e1.attributes();
        //2.4得到所有属性对象 返回迭代器
        Iterator<Attribute> it = e1.attributeIterator();

    }
    /**
     * *
     * description:获取文本
     */
    public static void test2(){
        Element ele = doc.getRootElement();
        //空格和换行也是xml的内容
        String content = ele.getText();
        System.out.println("========="+content);
        //获取文本  先获取表签在获取标签上的文本
        Element e1 = ele.element("bean");
        //1.得到文本
        String text = e1.getText();
        //2.得到指定子标签名的文本内容
        Element e2 = e1.element("property");
        String textValue = e2.getText();    //得到标签的文本内容 
    }
    /**
     * *
     * description:输出xml
     * @param e
     * @param sb
     */
    public static void printXML(Element e,StringBuffer sb){

        sb.append("<"+e.getName() );
        List<Attribute> list = e.attributes();
        if(list!=null&&list.size()>0){
            for(Attribute a : list){
                sb.append(" "+a.getName()+"="+"\""+a.getValue()+"\"");              
            }
            sb.append(">"); 
        }
        Iterator<Node> it = e.nodeIterator();
        while(it.hasNext()){
            Node node = it.next();
            if(node instanceof Element){
                printXML((Element)node,sb);
            }
            if(node instanceof Text){
                Text text = (Text)node;
                sb.append(text.getText());
            }
        }
        System.out.println("</"+e.getName()+">");
        sb.append("</"+e.getName()+">" );

    }
    /**
     * 
     * description:把xml文档信息封装到对象中
     */
    public static void test3(){
        Element ele = doc.getRootElement();
        Iterator<Element>  it = ele.elementIterator("bean");

        while(it.hasNext()){
            Element e = it.next();
            String str = e.elementText("property");//读取当前表签下的propert标签的文本
            System.out.println(str);
            break;
        }
    }
    /**
     * *
     * description:拷贝xml
     * @throws Exception
     */
    public static void test4() throws Exception{
        //创建xml解析器
        //SAXReader reader = new SAXReader();
        //读取xml文件 生成Document对象
        Document document = reader.read(new File("./src/struts.xml"));


        FileOutputStream out = new FileOutputStream(new File("D:/struts.xml"));
        XMLWriter write = new XMLWriter(out);
        write.write(document);//写
        write.close();
    }   
    /**
     * *
     * description:拷贝xml
     * @throws Exception
     */ 
    public static void test5() throws Exception{
        //创建xml解析器
        //SAXReader reader = new SAXReader();
        //读取xml文件 生成Document对象
        Document document = reader.read(new File("./src/struts.xml"));      
        FileOutputStream out = new FileOutputStream(new File("D:/struts2.xml"));
        OutputFormat of = OutputFormat.createCompactFormat();//设置紧凑的格式
        OutputFormat of1 = OutputFormat.createPrettyPrint();//设置漂亮的格式
        XMLWriter write = new XMLWriter(out,of1);
        write.write(document);
        write.close();
    }       

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值