在Android应用程序中解析XML文件

时间:2013-09-24 07:05:21

标签: android android-asynctask

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

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

相同的代码,如果我作为Java应用程序运行来解析XML,则需要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解析器。

1 个答案:

答案 0 :(得分: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");