从android中的网络数据库解析xml

发布于 2024-10-27 05:44:38 字数 67 浏览 0 评论 0原文

在我的应用程序中,当我单击按钮时,我想显示一个存储在 url 数据库中的 xml 文件,如何做到这一点,请给我一个例子。

in my app when i click a button, i want to show an xml file which is been stored in a database of an url, how to do this, pls give me an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2024-11-03 05:44:38

让服务器端脚本获取服务器上的数据并将其作为 XML 返回。然后下载该页面并将其加载到您的应用程序中。

使用此代码,您可以从在线数据库获取 xml 文件,并将其解析为 Android 中的 xml 文档。

Document xmlDocument = fromString(downloadPage("http://example.com/data.php");

这是一个简短的脚本,应该下载网页并将其作为字符串返回。

public String downloadPage(String targetUrl)
{
    BufferedReader in = null;
    try
    {
        // Create a URL for the desired page
        URL url = new URL(targetUrl);

        // Read all the text returned by the server
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        String output = "";
        while ((str = in.readLine()) != null)
        {
            // str is one line of text; readLine() strips the newline
            // character(s)
            output += "\n";
            output += str;
        }
        return output.substring(1);
    }
    catch (MalformedURLException e)
    {}
    catch (IOException e)
    {}
    finally
    {
        try
        {
            if (in != null) in.close();
        }
        catch (IOException e)
        {

        }
    }
    return null;
}

这是一个简单的 DOM 解析器,用于将字符串解析为 Document 对象。

public static Document fromString(String xml)
{
    if (xml == null)
        throw new NullPointerException("The xml string passed in is null");

    // from http://www.rgagnon.com/javadetails/java-0573.html
    try
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));

        Document doc = db.parse(is);

        return doc;
    }
    catch (SAXException e)
    {
        return null;
    }
    catch(Exception e)
    {
        CustomExceptionHandler han = new CustomExceptionHandler();
        han.uncaughtException(Thread.currentThread(), e);
        return null;
    }
}

Have a server side script get the data on the server and return it as XML. Then download the page and load it into your application.

Using this code you can get an xml file from an online database and parse it as a xml Document in Android.

Document xmlDocument = fromString(downloadPage("http://example.com/data.php");

This is a short script that should download a webpage and return it as a string

public String downloadPage(String targetUrl)
{
    BufferedReader in = null;
    try
    {
        // Create a URL for the desired page
        URL url = new URL(targetUrl);

        // Read all the text returned by the server
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        String output = "";
        while ((str = in.readLine()) != null)
        {
            // str is one line of text; readLine() strips the newline
            // character(s)
            output += "\n";
            output += str;
        }
        return output.substring(1);
    }
    catch (MalformedURLException e)
    {}
    catch (IOException e)
    {}
    finally
    {
        try
        {
            if (in != null) in.close();
        }
        catch (IOException e)
        {

        }
    }
    return null;
}

This is a simple DOM parser to parse a string into a Document object.

public static Document fromString(String xml)
{
    if (xml == null)
        throw new NullPointerException("The xml string passed in is null");

    // from http://www.rgagnon.com/javadetails/java-0573.html
    try
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));

        Document doc = db.parse(is);

        return doc;
    }
    catch (SAXException e)
    {
        return null;
    }
    catch(Exception e)
    {
        CustomExceptionHandler han = new CustomExceptionHandler();
        han.uncaughtException(Thread.currentThread(), e);
        return null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文