您的位置:首页 > Web前端 > JavaScript

搜索功能优化,性能优化比较,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次之。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: