xml|xml String字符串解析 map转xml

最近和其他系统调试接口,用的webServices 传参和返回类型要求都为String ,所以双方约定为xml格式的字符串,使用工具为dom4j具体代码如下所示:



package com.cee.tyrz.services.ldap; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.UnknownHostException; import java.rmi.RemoteException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import javax.naming.NamingException; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.rpc.ServiceException; import javax.xml.soap.MessageFactory; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.jdom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import com.cee.common.CallBack; import com.cee.common.CallService; import com.cee.common.DESPlus; import com.cee.common.MD5Code; import com.cee.common.SpringContextHelper; import com.cee.common.XmlUtils; import com.cee.common.util.ConfigFactory; import com.cee.common.util.LoadProperties; import com.cee.components.ldap.LdapHelp; import com.cee.sys.service.SysOrganizationsService; import com.cee.sys.service.SysUsersService; import com.cee.tyrz.services.ad.AdLdapContext; import com.cee.tyrz.services.ad.AdService; import com.cee.tyrz.services.ad.AdServiceImp; import com.cee.tyrz.services.hrservice.HRService; import com.cee.tyrz.services.hrservice.HRServiceImpl; import com.cee.tyrz.services.ldap.support.PinyinHandler; import com.cee.tyrz.warning.service.PushWarningService; import com.cee.tyrz.yhfp.dao.SsoModelDAO; import com.cee.tyrz.yhfp.domain.EipSsoModle; import com.cee.tyrz.yhfp.service.CoremailService; import com.cee.tyrz.yhfp.service.ISsoModelService; import com.cee.tyrz.yhfp.service.yhfpManager; import com.cee.tyrz.yhfp.service.impl.LoadUserService; import com.sun.org.apache.bcel.internal.generic.LADD; import com.sun.xml.messaging.saaj.util.JAXMStreamSource; /** * 具体实现ldap发布接口 * * @author songyj * */ public class LdapServiceExtendImp implements LdapServiceExtend { static Properties ad = LoadProperties.loadProperties("/config/sysParameter.properties"); static Logger logger = Logger.getLogger(LdapServiceExtendImp.class.getName()); publicString getListByUserId(String userIdXml) { List userIdList = readStringXml(userIdXml); Map resultMap = new HashMap(); String resultXml = ""; ConfigFactory.loadAllConfigFile(""); int yyy = 0; int nnn = 0; for(String userIdStr : userIdList){ List map=LdapHelp.searchForListMap("cn=Person,dc=sinopharm,dc=com", 2, "(userid=" + userIdStr + ")", null, null, null); if(map != null && map.size()> 0){ try { resultMap.put(userIdStr, map.get(0).get("cn")); yyy += 1; } catch (Exception e) { e.printStackTrace(); continue; }}else{ resultMap.put(userIdStr, ""); nnn += 1; } } if(null != resultMap && resultMap.size() > 0){ try { System.out.println(resultMap.size()); resultXml = map2xml(resultMap); } catch (IOException e) { e.printStackTrace(); } }System.out.println(resultXml); System.out.println("=============================="+resultMap.size()+"yyy="+yyy+"nnn"+nnn); return resultXml; } publicString map2xml(Map map) throws IOException { Document d = DocumentHelper.createDocument(); org.dom4j.Element root = d.addElement("xml"); Set keys = map.keySet(); for(String key:keys) { org.dom4j.Element line = root.addElement("line"); line.addElement("userid").addText(key); line.addElement("username").addText(map.get(key)); } StringWriter sw = new StringWriter(); XMLWriter xw = new XMLWriter(sw); xw.setEscapeText(false); xw.write(d); String xmlStr = sw.toString(); return sw.toString(); } publicList readStringXml(String xml) { Document doc = null; List userIdList = new ArrayList(); try { doc = DocumentHelper.parseText(xml); // 将字符串转为XMLorg.dom4j.Element rootElt = doc.getRootElement(); // 获取根节点 System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称Iterator iter = rootElt.elementIterator("line"); // 获取根节点下的子节点head// 遍历head节点 while (iter.hasNext()) {org.dom4j.Element recordEle =(org.dom4j.Element) iter.next(); String userid = ((org.dom4j.Element) recordEle).elementTextTrim("userid"); // 拿到head节点下的子节点title值 //System.out.println("userid:" + userid); userIdList.add(userid); }} catch (DocumentException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return userIdList; } public static void main(String[] args) { //String testXml = "1104847411045610110659931101664311047989110906871109070311114687110580491101875611020196110544951102177911019807111121081105867811049993110166331104563011032085110496791105092611058733110586791105871111058724110490171104837911049050110323571109087311067403110954161109534011095513110954441109544611091209110954091106556911049004110954881108478311110908"; LdapServiceExtendImp imp = new LdapServiceExtendImp(); String resultXml = imp.getListByUserId(testXml); System.out.println(resultXml); } }



【xml|xml String字符串解析 map转xml】

    推荐阅读