zoukankan      html  css  js  c++  java
  • org.dom4j.Document相关操作api

    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    import org.apache.commons.io.IOUtils;
    import org.dom4j.Node;
    import org.doom4j.DocumentHelper;


    //文件流InputStream 转Document,转Element
    public Element parseXml(InputStream stream){
    SAXReader reader=new SAXReader();
    Document document;
    try {
    document = reader.read(stream);
    Element root=document.getRootElement();//Element为根结点
    return root;
    } catch (DocumentException e) {
    throw new RuleException(e);
    }
    }

    //文件流InputStream 转 XMl格式文件
    String xml = IOUtils.toString(inputStream,"utf-8");
    //Document 转 InputStream
    InputStream stream = IOUtils.toInputStream(document.asXML())

    //Document 直接根据Element解析
    Document doc = new Document();//
    List<Element> oldEls = doc.getRootElement().elements();//获得Element集合后进行其他操作

    //Document转 Xml格式文件
    Document doc = new Document();//实际情况 doc可根据其他方式获得
    String xml = doc.asXML();


    //Xml 转Document对象
    Document doc = DocumentHelper.parseText(xml);

    //Elemnt 转XMl
    Element element ;
    Document doc = element.getDocument();
    String xml = doc.asXML();


    听说学习能够让青春永驻。
  • 相关阅读:
    Ansible中文权威指南学习
    gitlab
    Python-Day01-task
    Python-Day01
    CentOs6.7 python2.6升级到2.7.11
    网站访问慢排查方法及解决方案
    LAMP环境下zabbix安装配置
    监控知识点概述
    Java抽象类、接口和内部类
    Java 访问控制
  • 原文地址:https://www.cnblogs.com/chenyf/p/9121953.html
Copyright © 2011-2022 走看看