首页 > Web开发 > 详细

W3C Document 与Jsoup Document之间对象互转

时间:2015-10-09 02:10:01      阅读:911      评论:0      收藏:0      [点我收藏+]

? ? 1. org.w3c.dom.Document对象转成org.jsoup.nodes.Document对象:

? ? ??

String fragment = "<data><employee><name id=\"1\">益达</name><name id=\"2\">yida</name>"
				+ "<title>Manager</title></employee></data>";

XMLUtils util = new XMLUtils();
//先得到W3C Document对象
Document doc = util.fragment2Document(fragment);
		
//将W3C Document对象转成XML字符串
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
System.out.println(writer.toString());

? ? 然后XML String to jsoup Document对象,so easy!

//这里xmlString表示xml字符串代码片段
org.jsoup.nodes.Document document = org.jsoup.Jsoup.parse(xmlString);

?

?

? ? 2.org.jsoup.nodes.Document对象转成org.w3c.dom.Document对象:

org.jsoup.helper.W3CDom w3cDom = new W3CDom();
//这里的doc对象指的是jsoup里的Document对象
org.w3c.dom.Document w3cDoc = w3cDom.fromJsoup(doc);

?

? ?注意:org.jsoup.helper.W3CDom类在jsoup-1.7.x.jar里是不存在的,请使用jsoup-1.8.3.jar,特此提醒!谢谢!

W3C Document 与Jsoup Document之间对象互转

原文:http://iamyida.iteye.com/blog/2247570

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!