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


posted @ 2018-06-01 15:40  chen_yf  阅读(5921)  评论(0编辑  收藏  举报