package cn.com.guju.util; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class XmlUtil { public static void parserXmlString(String inputXml) { SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(new ByteArrayInputStream( inputXml.getBytes("UTF-8"))); Element xml = document.getRootElement(); for (Iterator<Element> i = xml.elementIterator(); i.hasNext();) { Element info = i.next(); System.out.println(info.getName()+":"+info.getData()); } } catch (DocumentException | UnsupportedEncodingException e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { String xml = "<xml><ToUserName><![CDATA[gh_cc18c38ec023]]></ToUserName><FromUserName><![CDATA[okPKJjjEeML-0szW0Gg1ITKPZiW4]]></FromUserName><CreateTime>1426741701</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[CLICK]]></Event><EventKey><![CDATA[houseType]]></EventKey></xml>"; parserXmlString(xml); } }
版权声明:本文博主原创文章,博客,未经同意不得转载。