搜索功能优化,性能优化比较,dom,dom4j,json

问题由来: 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次之。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值