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



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM