搜索功能优化,性能优化比较,dom,dom4j,json
2015-12-31 16:25
579 查看
问题由来: java开发时,有时候要解析xml,我常用处理xml有3种方法,dom,dom4j,json,如何做到开发中又快又好呢!?
项目需求: 实现搜索联系人功能
项目代码:如下
dom4j与dom:
package org.jivesoftware.spark.ui.uiutil;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.salesoa.orgtree.entity.Organ;
import com.salesoa.orgtree.entity.UserEntity;
public class xmltoolaccount {
// public static NodeList accountStrlao;
// public static NodeList userNameStrlao;
// public static HashMap<String, String> userNameMap =new HashMap<String,String >();
// public static HashMap<String, String> accountMap =new HashMap<String,String >();
// public static ArrayList<String> userNameList =new ArrayList<String>();
// public static ArrayList<String> accountList =new ArrayList<String>();
public static ArrayList<String> userNameList=null;
public static ArrayList<String> accountList=null;
public static void initialValXmlaccount(String accountInput,int showLenght) {
accountInput.trim();
userNameList=null;
accountList=null;
userNameList =new ArrayList<String>();
accountList =new ArrayList<String>();
int haveGetint = 0;
int lengaccountStr = accountInput.length();
// lengUserNameStr = lengUserNameStr -1;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
// com.salesoa.orgtree.TreeDemo.document = db.parse( com.salesoa.orgtree.TreeDemo.f);
// Element element = com.salesoa.orgtree.TreeDemo.document1.getDocumentElement(); //dom
org.dom4j.Element root = com.salesoa.orgtree.TreeDemo. document2.getRootElement() ;
// NodeList childNodes1 = element.getChildNodes();//Organization n
// NodeList childNodes_Organization = element.getElementsByTagName("Organization");//dom
// Element Node_Organization = (Element)childNodes_Organization.item(0);
// NodeList childNodes_users = Node_Organization.getElementsByTagName("users");
// Element Node_users = (Element)childNodes_Organization.item(0);
List childNodes_user = root.elements("user"); //dom4j
int i=0;
for (Iterator it = childNodes_user.iterator(); it.hasNext();) {
i++;
System.out.println(String.valueOf(i));
org.dom4j.Element elm = (org.dom4j.Element) it.next();
if(elm.element("account")!=null){
String accnt= elm.element("account").getText();
if(accnt.length()<lengaccountStr){continue;}
String accntSub = accnt.substring(0, lengaccountStr);
if(accntSub .equals(accountInput)){
haveGetint +=1;
accountList.add(accnt);
if(elm.element("userName")!=null){
String usrnm= elm.element("userName").getText();
userNameList.add(usrnm);
}
}
if(haveGetint >showLenght ){return ;}
}
}
json:
public class CopyOfxmltoolaccount {
public static ArrayList<String> userNameList=null;
public static ArrayList<String> accountList=null;
public static JSONArray userjsonarray=null;
public static JSONObject joo =null;
public static int haveGetint = 0;
public static int lengaccountStr = 0;
public static String accntSub ="";
public static String accnt="";
public static void initialValXmlaccount(String accountInput,int showLenght) {
accountInput.trim();
userNameList=null;
accountList=null;
userjsonarray=null;
userNameList =new ArrayList<String>();
accountList =new ArrayList<String>();
haveGetint = 0;
lengaccountStr = accountInput.length();
try {
// long lasting = System.currentTimeMillis();
userjsonarray = JSONArray.fromObject(com.salesoa.orgtree.srchurjson.json);
accntSub="";
accnt="";
joo=null;
for(int m = 0; m<userjsonarray.size();m++){
joo = userjsonarray.getJSONObject(m);
if(joo.containsKey("account")){
accnt= joo.getString("account");
if(accnt.length()<lengaccountStr){continue;}
accntSub = accnt.substring(0, lengaccountStr);
if(accntSub .equals(accountInput)){
haveGetint +=1;
accountList.add(accnt);
if(joo.containsKey("userName")){
// String usrnm= joo.getString("userName");
userNameList.add(joo.getString("userName"));
}
}
if(haveGetint >showLenght ){return ;}
}
}
// System.out.println("运行时间3+:" + (System.currentTimeMillis() - lasting) + "毫秒");
}
总结:json 最好,dom4j次之。
项目需求: 实现搜索联系人功能
项目代码:如下
dom4j与dom:
package org.jivesoftware.spark.ui.uiutil;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.salesoa.orgtree.entity.Organ;
import com.salesoa.orgtree.entity.UserEntity;
public class xmltoolaccount {
// public static NodeList accountStrlao;
// public static NodeList userNameStrlao;
// public static HashMap<String, String> userNameMap =new HashMap<String,String >();
// public static HashMap<String, String> accountMap =new HashMap<String,String >();
// public static ArrayList<String> userNameList =new ArrayList<String>();
// public static ArrayList<String> accountList =new ArrayList<String>();
public static ArrayList<String> userNameList=null;
public static ArrayList<String> accountList=null;
public static void initialValXmlaccount(String accountInput,int showLenght) {
accountInput.trim();
userNameList=null;
accountList=null;
userNameList =new ArrayList<String>();
accountList =new ArrayList<String>();
int haveGetint = 0;
int lengaccountStr = accountInput.length();
// lengUserNameStr = lengUserNameStr -1;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
// com.salesoa.orgtree.TreeDemo.document = db.parse( com.salesoa.orgtree.TreeDemo.f);
// Element element = com.salesoa.orgtree.TreeDemo.document1.getDocumentElement(); //dom
org.dom4j.Element root = com.salesoa.orgtree.TreeDemo. document2.getRootElement() ;
// NodeList childNodes1 = element.getChildNodes();//Organization n
// NodeList childNodes_Organization = element.getElementsByTagName("Organization");//dom
// Element Node_Organization = (Element)childNodes_Organization.item(0);
// NodeList childNodes_users = Node_Organization.getElementsByTagName("users");
// Element Node_users = (Element)childNodes_Organization.item(0);
List childNodes_user = root.elements("user"); //dom4j
int i=0;
for (Iterator it = childNodes_user.iterator(); it.hasNext();) {
i++;
System.out.println(String.valueOf(i));
org.dom4j.Element elm = (org.dom4j.Element) it.next();
if(elm.element("account")!=null){
String accnt= elm.element("account").getText();
if(accnt.length()<lengaccountStr){continue;}
String accntSub = accnt.substring(0, lengaccountStr);
if(accntSub .equals(accountInput)){
haveGetint +=1;
accountList.add(accnt);
if(elm.element("userName")!=null){
String usrnm= elm.element("userName").getText();
userNameList.add(usrnm);
}
}
if(haveGetint >showLenght ){return ;}
}
}
json:
public class CopyOfxmltoolaccount {
public static ArrayList<String> userNameList=null;
public static ArrayList<String> accountList=null;
public static JSONArray userjsonarray=null;
public static JSONObject joo =null;
public static int haveGetint = 0;
public static int lengaccountStr = 0;
public static String accntSub ="";
public static String accnt="";
public static void initialValXmlaccount(String accountInput,int showLenght) {
accountInput.trim();
userNameList=null;
accountList=null;
userjsonarray=null;
userNameList =new ArrayList<String>();
accountList =new ArrayList<String>();
haveGetint = 0;
lengaccountStr = accountInput.length();
try {
// long lasting = System.currentTimeMillis();
userjsonarray = JSONArray.fromObject(com.salesoa.orgtree.srchurjson.json);
accntSub="";
accnt="";
joo=null;
for(int m = 0; m<userjsonarray.size();m++){
joo = userjsonarray.getJSONObject(m);
if(joo.containsKey("account")){
accnt= joo.getString("account");
if(accnt.length()<lengaccountStr){continue;}
accntSub = accnt.substring(0, lengaccountStr);
if(accntSub .equals(accountInput)){
haveGetint +=1;
accountList.add(accnt);
if(joo.containsKey("userName")){
// String usrnm= joo.getString("userName");
userNameList.add(joo.getString("userName"));
}
}
if(haveGetint >showLenght ){return ;}
}
}
// System.out.println("运行时间3+:" + (System.currentTimeMillis() - lasting) + "毫秒");
}
总结:json 最好,dom4j次之。
相关文章推荐
- 【学习笔记javascript设计模式与开发实践(职责链模式)----13】
- 对javascript中原型链的理解
- js设置cookie
- 三种方法实现js跨域访问
- JSON简介以及用法汇总
- 【JavaScript 从零开始】变量作用域
- javascript进击(三)简介
- JavaScript可以使用try...catch来进行异常处理
- 21个值得收藏的Javascript技巧
- 解析Json的谷歌官方方法Gson和阿里巴巴的fastJson方法。
- 跨域访问-JSONP
- javascript进击(一)简介
- js中几种实用的跨域方法原理详解
- Json文件放入Assets文件,读取解析并且放入listview中显示。
- js模拟html锚链接
- javascript禁止复制与粘贴
- JS编码解码
- js判断上传文件大小
- js控制手机号码中间用星号代替
- 从setTimeout谈js运行机制