如何高效/顺利地解析XML?

时间:2013-01-02 06:33:03

标签: android xml sax

我有一个像这样的在线XML:

http://myxml.com/list.xml

<rootxml>
<url src="sdfsdfsdfsdfs" />
<url src="sdfsdfsdfsdfs" />
<url src="sdfsdfsdfsdfs" />
<url src="sdfsdfsdfsdfs" />
<url src="sdfsdfsdfsdfs" />
<url src="sdfsdfsdfsdfs" />
<url src="sdfsdfsdfsdfs" />
and so on...
</rootxml>

它包含数千个,我尝试在这个上使用SAXParser,但解析XML需要30-40秒。我的示例应用程序非常慢。

3 个答案:

答案 0 :(得分:1)

public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

获取XML内容后,我们需要获取XML文件的DOM元素。下面的函数将解析XML内容,并为您提供DOM元素。

public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
            // return DOM
        return doc;
}

通过传递元素节点名称

获取每个xml子元素值
     static final String KEY_ITEM = "URL"; // parent node


    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(rootitem);

    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {

   Node node = nl.item(i);
            Element e = (Element) node;
            NodeList tag = e.getElementsByTagName("url");
            Element element = (Element) tag.item(0);
            tag = element.getChildNodes();` // name child value

                 String value = ((Node) categoryList.item(0)).getNodeValue().toString();

答案 1 :(得分:0)

String Xmlresponse=getXmlFromUrl(url);
Document mDocument =getDomElement(Xmlresponse);
NodeList mNodeResponse = mDocument.getElementsByTagName("rootxml");

for (int i = 0; i < mNodeResponse.getLength(); i++) {

Element e = (Element) mNodeResponse.item(i);
 String url=getValue(e, "url");
}



private String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

获取xml响应后获取文档元素

//here pass String xml is your response
private Document getDomElement(String xml) {
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }

    return doc;
}


public final static String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child
                    .getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}



public static String getValue(Element item, String str) {
    NodeList n = item.getElementsByTagName(str);
    return getElementValue(n.item(0));
}

答案 2 :(得分:0)

谢谢大家的帮助。在这个花了将近10个小时之后,我意识到我真正想要的是属性值而不是节点的实际值。通过使用Saching建议的代码,我想出了以下解决方案:

for (int i = 0; i < nl.getLength(); i++){   

            Element e = (Element) nl.item(i);

            String value = e.getAttribute("src");

        }

它有效。再次感谢。