Advertisement
LookedPath

XMLParser.java

Nov 4th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package com.lookedpath.firstlesson;
  2.  
  3. import java.io.IOException;
  4. import java.io.StringReader;
  5. import java.io.UnsupportedEncodingException;
  6.  
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10.  
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.ClientProtocolException;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.apache.http.util.EntityUtils;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.Element;
  19. import org.w3c.dom.Node;
  20. import org.w3c.dom.NodeList;
  21. import org.xml.sax.InputSource;
  22. import org.xml.sax.SAXException;
  23.  
  24. import android.util.Log;
  25.  
  26.  
  27.  
  28. public class XMLParser {
  29.     public String getXmlFromUrl(String url) {
  30.         String xml = null;
  31.  
  32.         try {
  33.             // defaultHttpClient
  34.             DefaultHttpClient httpClient = new DefaultHttpClient();
  35.             HttpPost httpPost = new HttpPost(url);
  36.  
  37.             HttpResponse httpResponse = httpClient.execute(httpPost);
  38.             HttpEntity httpEntity = httpResponse.getEntity();
  39.             xml = EntityUtils.toString(httpEntity);
  40.  
  41.         } catch (UnsupportedEncodingException e) {
  42.             e.printStackTrace();
  43.         } catch (ClientProtocolException e) {
  44.             e.printStackTrace();
  45.         } catch (IOException e) {
  46.             e.printStackTrace();
  47.         }
  48.         // return XML
  49.         return xml;
  50.     }
  51.    
  52.     public Document getDomElement(String xml){
  53.         Document doc = null;
  54.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  55.         try {
  56.  
  57.             DocumentBuilder db = dbf.newDocumentBuilder();
  58.  
  59.             InputSource is = new InputSource();
  60.                 is.setCharacterStream(new StringReader(xml));
  61.                 doc = db.parse(is);
  62.  
  63.             } catch (ParserConfigurationException e) {
  64.                 Log.e("Error: ", e.getMessage());
  65.                 return null;
  66.             } catch (SAXException e) {
  67.                 Log.e("Error: ", e.getMessage());
  68.                 return null;
  69.             } catch (IOException e) {
  70.                 Log.e("Error: ", e.getMessage());
  71.                 return null;
  72.             }
  73.                 // return DOM
  74.             return doc;
  75.     }
  76.     public String getValue(Element item, String str) {
  77.         NodeList n = item.getElementsByTagName(str);
  78.         return this.getElementValue(n.item(0));
  79.     }
  80.      
  81.     public final String getElementValue( Node elem ) {
  82.              Node child;
  83.              if( elem != null){
  84.                  if (elem.hasChildNodes()){
  85.                      for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
  86.                          if( child.getNodeType() == Node.TEXT_NODE  ){
  87.                              return child.getNodeValue();
  88.                          }
  89.                      }
  90.                  }
  91.              }
  92.              return "";
  93.       }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement