使用Dom4j实现XML字符串与Docment对象的相互转换
01 | import org.xml.sax.InputSource; |
02 | import org.xml.sax.SAXException; |
03 | //导入dom4j包,可以到官网上去下载后导入 |
04 | import org.dom4j.DocumentException; |
05 | import org.dom4j.DocumentHelper; |
06 | import org.dom4j.Element; |
07 | import org.w3c.dom.Document; |
13 | public class Dom4jTest { |
15 | public org.dom4j.Document StrToXMLDocment(String xmlStr) { //Str是传入的一段XML内容的字符串 |
16 | org.dom4j.Document document = null ; |
19 | document = DocumentHelper.parseText(xmlStr); //DocumentHelper.parseText(str)这个方法将传入的XML字符串转换处理后返回一个Document对象 |
20 | } catch (DocumentException e) { |
26 | public String XMLDocumentToStr(Document document){ |
27 | String XMLStr = document.asXML(); //obj.asXML()则为Document对象转换为字符串方法 |
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。