2012-01-13 4 views
1

J'ai un fichier xml dans un endroit distant.Je veux utiliser ce fichier xml dans mon projet android.can quelqu'un me donne un exemple de code pour cela.J'utilise Android 2.2. P.: Je peux accéder au fichier xml local qui se trouve dans le dossier/res. Je ne sais rien à propos de xPath.lire le fichier xml distant dans android

+0

Quelque chose d'intéressant ... Si vous voulez définir comme la mise en page dans votre activité alors je pense que vous ne pouvez pas .. – user370305

+0

c'est juste un fichier xml contient des data.Here premières ne suis pas prendre sur la mise en page/manifeste xml. – advishnuprasad

+0

Ensuite, vous pouvez l'utiliser par analyseur XML valide .. – user370305

Répondre

2
try { 
      //set the download URL, a url that points to a file on the internet 
      //this is the file to be downloaded 
      URL url = new URL("http://IP/Downloads/data.xml"); 

      //create the new connection 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 



      //and connect! 
      urlConnection.connect(); 

      //set the path where we want to save the file 
      //in this case, going to save it on the root directory of the 
      //sd card. 
      File SDCardRoot = Environment.getExternalStorageDirectory(); 
      //create a new file, specifying the path, and the filename 
      //which we want to save the file as. 
      File file = new File(SDCardRoot,"data.xml"); 

      //this will be used to write the downloaded data into the file we created 
      FileOutputStream fileOutput = new FileOutputStream(file); 

      //this will be used in reading the data from the internet 
      InputStream inputStream = urlConnection.getInputStream(); 

      //this is the total size of the file 
      int totalSize = urlConnection.getContentLength(); 
      progressDialog.setMax(totalSize); 

      //variable to store total downloaded bytes 
      int downloadedSize = 0; 

      //create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; //used to store a temporary size of the buffer 

      //now, read through the input buffer and write the contents to the file 
      while ((bufferLength = inputStream.read(buffer)) > 0) { 
        //add the data in the buffer to the file in the file output stream (the file on the sd card 
        fileOutput.write(buffer, 0, bufferLength); 
        //add up the size so we know how much is downloaded 
        downloadedSize += bufferLength; 

      } 
      //close the output stream when done 
      fileOutput.close(); 
      //catch some possible errors... 
    } catch (MalformedURLException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 

Essayez ce code pour le téléchargement de votre fichier xml et vérifiez ce tutoriel pour la lecture xml http://www.java-samples.com/showtutorial.php?tutorialid=152

+0

au lieu d'enregistrer xml dans ma carte SD l'utiliser dynamiquement pour ma recherche query.ty fr le lien – advishnuprasad

+0

Couldnt get u .. ?? que voulez-vous dire par là ** dynamiquement pour ma requête de recherche. ** –

0

Si vous essayez de définir la mise en page ou dessinable ou le style par XML distant, vous ne pouvez pas.

+0

Non. Je veux juste utiliser les données xml à d'autres fins. Ne pas mettre en page – advishnuprasad

+0

Ok, puis utilisez la connexion http pour obtenir ce xml en réponse, et l'analyser. – jeet

+1

exactement ... là seulement j'ai un problème. URL url = nouvelle URL ("http://www.something.com/vishnu.xml"); urlConnection = (HttpsURLConnection) url.openConnection(); InputStream in = nouveau BufferedInputStream (urlConnection.getInputStream()); – advishnuprasad

0

meilleure façon de le lire dans la chaîne et faire tout ce que vous voulez faire. Public String getXmlFromUrl (String url) {
Chaîne xml = null;

try 
{ 
    //default http client 
    HttpClient httpClient = new DefaultHttpClient(); 

    HttpPost httpPost = new HttpPost(url); 

    System.out.println("URL IN PARSER:==="+url+"===="); 

    HttpResponse httpResponse = httpClient.execute(httpPost); 

    HttpEntity httpentity = httpResponse.getEntity(); 

    xml = EntityUtils.toString(httpentity); // I have changed it... because  occur while downloading.. 

    Log.d("response", xml); 
} 
catch(UnsupportedEncodingException e) 
{ 
    e.printStackTrace(); 
} 
catch (ClientProtocolException e) 
{ 
    e.printStackTrace(); 
} 
catch (IOException e) 
{ 
    e.printStackTrace(); 
} 


return xml; 

}

Questions connexes