浏览 2351 次
锁定老帖子 主题:自己写了个方法处理XML里的特殊字符
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-10-14
最后修改:2011-10-14
/** *实例 <?xml version="1.0" standalone="no"?><?Siebel-Property-Set EscapeNames="true"?><SiebelMessage IntObjectName="Sample Account"><ListOfSampleAccount><Account><Name>Tibco&XA</Name><Location>PaloAlto<&>dd</Location><Contact><Phone>029-88161086</Phone><Mail>www.163.com<&>dd</Mail></Contact></Account></ListOfSampleAccount></SiebelMessage> * */ import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class XmlParse { public static void XML2PropertySet(TdDataBean pInterface,Object pInPropSet, Object[] oOutPropSet, boolean isEAI){ try { org.jdom.filter.ContentFilter a = null; String xmlvalue = ((SiebelPropertySet)pInPropSet).getValue(); // StringReader read = new StringReader(xmlvalue); // InputSource source = new InputSource(read); // // SAXBuilder sb = new SAXBuilder(); String xml = deal(xmlvalue); Document doc = DocumentHelper.parseText(xml); Element element = doc.getRootElement(); if(isEAI == false) parseDoc(pInterface,element,oOutPropSet); else{ ((SiebelPropertySet)oOutPropSet[0]).reset(); ((SiebelPropertySet)oOutPropSet[0]).setProperty("XMLCharEncoding","UTF-16LE"); Object[] childProp = new Object[1]; pInterface.NewPropertySet(childProp); parseEAIDoc(pInterface,element,childProp); ((SiebelPropertySet)oOutPropSet[0]).addChild((SiebelPropertySet)childProp[0]); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static Object parseDoc(TdDataBean pInterface,Element element,Object[] oOutPropSet){ ((SiebelPropertySet)oOutPropSet[0]).setType(element.getName()); if(element.getText()!=null){ ((SiebelPropertySet)oOutPropSet[0]).setValue(element.getText()); } List attr = element.attributes(); if(attr != null){ for(int i=0;i<attr.size();i++){ ((SiebelPropertySet)oOutPropSet[0]).setProperty(((Attribute)attr.get(i)).getName(), ((Attribute)attr.get(i)).getValue()); } } List childrenEle = element.elements(); if(childrenEle!=null){ for(int j=0;j<childrenEle.size();j++){ Element childele=(Element) childrenEle.get(j); Object[] childProp = new Object[1]; pInterface.NewPropertySet(childProp); childProp[0] = parseDoc(pInterface,childele,childProp); ((SiebelPropertySet)oOutPropSet[0]).addChild((SiebelPropertySet)childProp[0]); } } return oOutPropSet[0]; } private static Object parseEAIDoc(TdDataBean pInterface,Element element,Object[] oOutPropSet){ ((SiebelPropertySet)oOutPropSet[0]).setType(element.getName()); if(element.getText()!=null){ ((SiebelPropertySet)oOutPropSet[0]).setValue(element.getText()); } List attr = element.attributes(); if(attr != null){ for(int i=0;i<attr.size();i++){ ((SiebelPropertySet)oOutPropSet[0]).setProperty(((Attribute)attr.get(i)).getName(), ((Attribute)attr.get(i)).getValue()); } } if(element.getName().equals("SiebelMessage")){ if(element.attribute("MessageId")==null || element.attribute("MessageId").equals("")){ ((SiebelPropertySet)oOutPropSet[0]).setProperty("MessageId",""); } ((SiebelPropertySet)oOutPropSet[0]).setProperty("IntObjectFormat","Siebel Hierarchical"); ((SiebelPropertySet)oOutPropSet[0]).setProperty("MessageType","Integration Object"); } List childrenEle = element.elements(); if(childrenEle!=null){ for(int j=0;j<childrenEle.size();j++){ Element childele=(Element) childrenEle.get(j); if(childele.elements()==null || childele.elements().size()==0){ ((SiebelPropertySet)oOutPropSet[0]).setProperty(childele.getName(),childele.getText()); }else{ Object[] childProp = new Object[1]; pInterface.NewPropertySet(childProp); childProp[0] = parseEAIDoc(pInterface,childele,childProp); ((SiebelPropertySet)oOutPropSet[0]).addChild((SiebelPropertySet)childProp[0]); } } } return oOutPropSet[0]; } public static String deal(String str) { Map <String,String> map = new HashMap<String,String>(); String temp = str; while(-1!=temp.indexOf("</")) { StringBuffer newbf = new StringBuffer(); StringBuffer oldbf = new StringBuffer(); int pos = temp.indexOf("</"); //int end = temp.indexOf(">"); String ss=temp.substring(pos+1,temp.length()); int k=ss.indexOf(">"); String nn=temp.substring(pos, pos+k+2); //System.out.println(nn); String xs="<"+nn.substring(nn.indexOf("</")+2, nn.length()); int m= temp.indexOf(xs); if(-1==m) { break; } //save the change str newbf.append(temp.substring(m, m+xs.length())); String replace = temp.substring(m+xs.length(), pos); newbf.append(transfer(replace)); newbf.append(nn); //save old str oldbf.append(temp.substring(m, m+xs.length())); oldbf.append(replace); oldbf.append(nn); if(null!=replace && replace.trim().length()>0) { map.put(oldbf.toString(), newbf.toString()); } temp = temp.substring(0, m)+temp.substring(pos+2+k, temp.length()); } for(String key:map.keySet()) { String back=map.get(key); str= replace(str,key,back); } System.out.println(str); return str; } public static String replace(String str,String str1,String str2) { int pos = str.indexOf(str1); str = str.substring(0,pos)+str2+str.substring(pos+str1.length(),str.length()); return str; } /** * change '<' to < * @param str * @return */ public static String transfer(String str) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); switch (c) { case '&': String str1=str.substring(i, str.length()); String str2 =str.substring(0, i); str1= str1.replaceFirst("[&]", "&"); str = str2+str1; break; case '<': str = str.replaceFirst("[<]", "<"); break; case '>': str = str.replaceFirst("[>]", ">"); break; case '"': str = str.replaceFirst("[\"]", """); break; case '‘': str = str.replaceFirst("[‘]", "'"); break; default: break; } } return str; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-10-14
放错版了,鉴定完毕。纯贴代码实在很难看。。。
|
|
返回顶楼 | |