如何在Android中进行xml解析?

时间:2013-05-06 07:41:00

标签: java android yahoo-api

  • 我正在Android中实现yahoo集成以检索所有内容 我的应用程序中的联系人。我有xml格式的网址,但我不知道如何 在Android中进行xml解析。请有人帮助我。这是我的 代码。

    private static final String KEY_CONTACT="contacts";
    private static final String KEY_FIELDS="fields";
    private static final String KEY_TYPE="type";
    private static final String KEY_VALUE="value";

 private void getAllContacts() {

            String host_url = "http://social.yahooapis.com/v1/user/" + mUSER_GUID+ "/contacts";
            String nonce = ""+System.currentTimeMillis();
            String timeStamp = ""+(System.currentTimeMillis()/1000L);

            try{
                String params = 
                        ""+encode("oauth_consumer_key")+"=" + encode(CONSUMER_KEY)
                        + "&"+encode("oauth_nonce")+"="+encode(nonce)
                        + "&"+encode("oauth_signature_method")+"="+encode("HMAC-SHA1")
                        + "&"+encode("oauth_timestamp")+"="+encode(timeStamp)
                        + "&"+encode("oauth_token")+"="+ACCESS_TOKEN
                        + "&"+encode("oauth_version")+"="+encode("1.0");


                String baseString = encode("GET")+"&"+encode(host_url)+"&"+encode(params);
               String signingKey = encode(CONSUMER_SECRET)+"&"+encode(ACCESS_TOKEN_SECRET);
              String lSignature = computeHmac(baseString, signingKey);
             lSignature = encode(lSignature);

                      String lRequestUrl = host_url
                                    + "?oauth_consumer_key="+CONSUMER_KEY
                                    + "&oauth_nonce="+nonce
                                    + "&oauth_signature_method=HMAC-SHA1"
                                    + "&oauth_timestamp="+timeStamp
                                    + "&oauth_token="+ACCESS_TOKEN
                                    + "&oauth_version=1.0"
                                    + "&oauth_signature="+lSignature
                                    ;

                HttpGet httpget = new HttpGet(lRequestUrl);
                HttpClient httpclient = new DefaultHttpClient();

               ResponseHandler<String> responseHandler = new BasicResponseHandler();


                String responseBody = httpclient.execute(httpget, responseHandler);

                SAXParserFactory spf=SAXParserFactory.newInstance();
                SAXParser sp=spf.newSAXParser();

                // Get the XMLReader of the SAXParser we created. 
                XMLReader xr=sp.getXMLReader();

                InputSource inputSource = new InputSource();
                inputSource.setEncoding("UTF-8");
                inputSource.setCharacterStream(new StringReader(responseBody));

                 xr.parse(inputSource);
                   Log.e("XML","READER");
     catch(Exception e)
            {
                e.printStackTrace();
                Log.e(TAG, "error while fetching user contacts");
            }

        }


 Thanks to you .               

2 个答案:

答案 0 :(得分:0)

Android本身推荐XmlPullParser,这是一种在Android上解析XML的高效且可维护的方法。

答案 1 :(得分:0)

如果使用SAX,则必须创建DefaultHandler并覆盖其解析XML文件的方法。

这是一个示例http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/