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();


    听说学习能够让青春永驻。
  • 相关阅读:
    类模板和函数模板
    vector用法
    the swap trick用于锐减过剩容量
    SIGHUP信号
    linux页表机制
    linux中sigsuspend和pause的区别
    最长回文串:LeetCode:Longest Palindromic Substring
    Implement strStr()
    ffmpeg知多少~~~
    下面可能会更新很多。。。
  • 原文地址:https://www.cnblogs.com/chenyf/p/9121953.html
Copyright © 2011-2022 走看看