Consume XML file from the Web

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Consume XML file from the Web

Postby mobilekid » Sat Oct 18, 2008 11:58 am

Hi,

I have been trying to get my head around that but haven't had much success.

Basically, I need to make a request to some resource on the web and manipulate its response (an xml file).

Here is some sample code. Once I have built the Document object I am not sure how to extract the values I need.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. DefaultHttpClient httpClient = new DefaultHttpClient();
  3.  
  4.  
  5.  
  6. HttpPost httpPost = new HttpPost("some URL);
  7.  
  8.  
  9.  
  10. HttpResponse response = httpClient.execute(httpPost);
  11.  
  12.  
  13.  
  14. HttpEntity entity = response.getEntity();
  15.  
  16.  
  17.  
  18. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  19.  
  20.  
  21.  
  22. DocumentBuilder db = dbf.newDocumentBuilder();
  23.  
  24.  
  25.  
  26. Document doc = db.parse(entity.getContent());
  27.  
  28.  
  29.  
  30. doc.getDocumentElement().normalize();
  31.  
  32.  
  33.  
  34. // Here I'd like to do smth with my XML.
  35.  
  36.  
  37.  
  38.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Thanks.
mobilekid
Freshman
Freshman
 
Posts: 7
Joined: Mon Aug 04, 2008 10:34 pm

Top

Postby shaildyp » Mon Oct 20, 2008 10:49 am

Try this :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                    try{
  3.  
  4.  
  5.  
  6.                         DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
  7.  
  8.                         DocumentBuilder parser = factory.newDocumentBuilder();
  9.  
  10.                         doc = parser.parse(new URL("http://www.example.com").openConnection().getInputStream());
  11.  
  12.                         if(null!=doc.getElementsByTagName("MyRoot))
  13.  
  14.                         if(null!=doc.getElementsByTagName("MyElement"))
  15.  
  16.                         {      
  17.  
  18.                           if(null!=doc.getElementsByTagName("myResponse"))
  19.  
  20.                           {
  21.  
  22.                                  NodeList nl =doc.getElementsByTagName("myResponse");
  23.  
  24.                                  s=nl.item(0).getFirstChild().getNodeValue();
  25.  
  26.                        
  27.  
  28.                           }
  29.  
  30.                         }      
  31.  
  32.                 } catch (ParserConfigurationException e) {
  33.  
  34.                        
  35.  
  36.                         Log("Error"+ e.getMessage());
  37.  
  38.                 } catch (FactoryConfigurationError e) {
  39.  
  40.                        
  41.  
  42.                         Log("Error"+e.getMessage());
  43.  
  44.                 } catch (SAXException e) {
  45.  
  46.                        
  47.  
  48.                         Log("Error"+ e.getMessage());
  49.  
  50.                 } catch (MalformedURLException e) {
  51.  
  52.                        
  53.  
  54.                         Log("Error"+ e.getMessage());
  55.  
  56.                 } catch (IOException e) {
  57.  
  58.                        
  59.  
  60.                         Log("Error"+e.getMessage());
  61.  
  62.                 }
  63.  
  64.                 catch(Exception e)
  65.  
  66.                 {
  67.  
  68.                        
  69.  
  70.                 }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
shaildyp
Freshman
Freshman
 
Posts: 5
Joined: Tue Sep 30, 2008 10:29 am
Location: India

Postby mobilekid » Tue Oct 21, 2008 9:04 pm

Thanks for the reply! As I was short of time I based my solution on the event-driven approach. It turned out that it is a way more efficient solutuion!
mobilekid
Freshman
Freshman
 
Posts: 7
Joined: Mon Aug 04, 2008 10:34 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests