0

我正在创建一个 Android 应用程序。在那里,我通过解析存储在移动设备SD 卡中的 XML 文件来显示结果。但是解析 XMl 文件需要超过一分钟的时间。

所以为此我已经实现了 AsyncTask 功能,但仍然需要超过一分钟。

如果我作为解析 XML 的 Java 应用程序运行相同的代码,则需要 3-5 秒来解析完整的 XML 并打印数据。

解析代码

public class ParseMyTripResponseXML {
    String aprovedStatus="";
    SetFlightRecordsData objFlight;
    public MyTripRespone parseMyTripXML(File filename) {
        MyTripRespone respObj = new MyTripRespone();

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = null;

        CorporateBookingApprovals corpApprovalObj = null;
        List<CorporateBookingApprovals> corpApprovalList = new ArrayList<CorporateBookingApprovals>();

        try {
            dBuilder = dbFactory.newDocumentBuilder();
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        Document doc = null;
        try {
            doc = dBuilder.parse(filename);
        }
        catch (SAXException e) {
            Log.d("SAXException", e.getMessage()+"");
        }
        catch (IOException e) {
            Log.d("IOException", e.getMessage()+"");
        }

        doc.getDocumentElement().normalize();
        Node rootNode = doc.getElementsByTagName("Root").item(0);
        Element rootElement = (Element) rootNode;
        String status = getTagValue("StatusCode", rootElement);
        if (status.equals("200")) {
            NodeList corpList = ((Element) doc.getElementsByTagName("Result").item(0)).getElementsByTagName("CorporateBookingApprovals");
            for (int i = 0; i < corpList.getLength(); i++) {
                try {
                    Node corporateDetailsNode = corpList.item(i);
                    Element corporateDetailsElement = (Element) corporateDetailsNode;
                    corpApprovalObj = new CorporateBookingApprovals();
                    corpApprovalObj.setApprovalId(getTagValue("ApprovalId", corporateDetailsElement));
                    corpApprovalObj.setAgentId(getTagValue("AgentId", corporateDetailsElement));
                    String statusApproval  = getTagValue("ApprovalStatus", corporateDetailsElement);
                    if(statusApproval.equals("approved")){
                        aprovedStatus = "A";
                    }else if (statusApproval.equals("pending")) {
                        aprovedStatus = "P";
                    }
                    else if (statusApproval.equals("inqueue")) {
                        aprovedStatus = "Q";
                    }
                    else if (statusApproval.equals("rejected")) {
                        aprovedStatus = "R";
                    }
                    corpApprovalObj.setApprovalStatus(aprovedStatus);
                    corpApprovalObj.setInsertTime(getTagValue("InsertTime", corporateDetailsElement));
                    objFlight = new SetFlightRecordsData();
                    objFlight.setFlightData(doc, corpApprovalObj);
                    corpApprovalList.add(corpApprovalObj);
                }
                catch (Exception e) {
                    Log.d("exception in main", e.getMessage()+"");
                    continue;
                }
            }
            respObj.setCoroprateBookingDetails(corpApprovalList);
        }

        return respObj;
    }

    public static String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
                .getChildNodes();
        Node nValue = (Node) nlList.item(0);
        if (nValue == null) {

            return "";
        }
        else {
            return nValue.getNodeValue();
        }
    }

我怎样才能让它更快?

我正在使用 DOM 解析器。

4

1 回答 1

0

我建议通过添加计时器函数和消除过程、注释掉部分并重新运行它来加快 XML 解析例程的一般方法,这样您就可以确切地知道是什么导致了速度变慢。通常的嫌疑人是对象创建调用和字符串处理。

long startTime;
long endTime;

startTime = android.os.SystemClock.uptimeMillis();

// parse routine here

endTime = android.os.SystemClock.uptimeMillis();

Log.d("xmlparse", "Excution time: "+(endTime-startTime)+" ms");
于 2013-09-24T07:24:33.920 回答