Android-Java-来自内容提供商的xml值

时间:2020-08-11 10:36:01

标签: java android android-contentprovider

我正在尝试从内容提供商获取数据,我可以将内容打印到屏幕上,下面是执行该操作的代码部分:

        if (DATA_CONTENT_URI != null && DATA_CONTENT_URI.length() != 0){
            Uri targURI = Uri.parse(DATA_CONTENT_URI);
            InputStream content = null;
            try {
                content = getContentResolver().openInputStream(targURI);
            } catch (FileNotFoundException e1) {
                Toast.makeText(getApplicationContext(), "Could not read the content. Please check the URI is correct", Toast.LENGTH_LONG).show();
                return;
            }

            BufferedReader reader1 = new BufferedReader(new InputStreamReader(content));
            String line1;
            try {
                while ((line1 = reader1.readLine()) != null) {
                    data.append(line1);
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }else{
            Toast.makeText(getApplicationContext(), "Please check your input", Toast.LENGTH_SHORT).show();
        }
        data.append("\n[]\n");

内容是XML,格式如下:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<a>
    <string name="foo1">bar1</string>
    <boolean name="foo2" value="bar2" />
</a>

我正在努力研究如何从XML中提取元素,而不是仅仅打印所有元素。我尝试了几件事,最终不得不放弃我制作的应用程序,因为我添加了很多废话,只是想使其正常工作。

任何输入都会很棒。

0 个答案:

没有答案
相关问题