【问题标题】:read the remote xml file in android在android中读取远程xml文件
【发布时间】:2012-01-13 05:20:44
【问题描述】:

我在远程有一个 xml 文件。我想在我的 android 项目中使用该 xml 文件。谁能给我一个示例代码。我使用的是 android 2.2。

P.S:我可以访问 /res 文件夹中的本地 xml 文件。 我对 xPath 一无所知。

【问题讨论】:

  • 有趣的事情......如果你想在你的活动中将它设置为布局,那么我认为你不能......
  • 这只是一个包含一些原始数据的 xml 文件。这里不讨论布局/清单 xml。
  • 那么就可以通过有效的xml解析器来使用了..
  • 好吧,我已经使用文档生成器和文档生成器工厂来访问 xml 的内容。我的问题是如何检索远程位置的 xml 文件。我使用了 HTTPUrlConnection,但它给出了运行时错误...

标签: android xml


【解决方案1】:
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();
    }

尝试使用此代码下载您的 xml 文件并查看此教程以读取 xml http://www.java-samples.com/showtutorial.php?tutorialid=152

【讨论】:

  • 我没有将 xml 保存在我的 sd 卡中,而是动态地将其用于我的搜索 query.ty fr 链接
  • 找不到你 ..?? 动态地对我的搜索查询是什么意思。
【解决方案2】:

如果您尝试通过远程 xml 设置布局或可绘制或样式,则不能。

【讨论】:

  • 不。我只是想将 xml 数据用于其他目的。不要设置布局
  • 好的,然后使用http连接获取该xml作为响应,并解析它。
  • 正是...只有我有问题。 URL url = new URL("something.com/vishnu.xml"); urlConnection = (HttpsURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream());
  • 如果您在 Android 2.2 或更高版本上进行开发,您可以利用 javax.xml.xpath 的功能(在 API 级别 8 中引入)。
  • ty...得到了答案...使用 SAXParser
【解决方案3】:

更好的方法是用 String 读取它并做任何你想做的事情。

public String getXmlFromUrl(String url) {
字符串 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;

}

【讨论】:

    猜你喜欢
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    相关资源
    最近更新 更多