使用java程序从链接下载xml文件

时间:2023-02-06 07:43:04

I am trying to create a Java program that starts up, downloads an xml file from a http web address http://api.eve-central.com/api/marketstat and saves it to a set location so I can then parse it for the data I want.

我正在尝试创建一个Java程序,该程序启动后,从http web地址http://api.eve-central.com/api/marketstat下载一个xml文件,并将其保存到一个设置好的位置,以便我可以解析它以获得所需的数据。

What I would like to know is how do I download this document from this link in java for a standard computer application?

我想知道的是,我如何从java的这个链接下载这个文档来获得一个标准的计算机应用程序?

3 个解决方案

#1


9  

Did you try the XML SAX parser provide by Java?

您尝试过Java提供的XML SAX解析器吗?

Here is a sample code:

下面是一个示例代码:

URL url = new URL("http://www.xyz.com/book.xml");
InputStream stream = url.openStream();
Document doc = docBuilder.parse(stream);

#2


1  

This function will get you the full content from the URL :

此功能将从URL获取完整内容:

public String getURLContent(String p_sURL)
{
    URL oURL;
    URLConnection oConnection;
    BufferedReader oReader;
    String sLine;
    StringBuilder sbResponse;
    String sResponse = null;

    try
    {
        oURL = new URL(p_sURL);
        oConnection = oURL.openConnection();
        oReader = new BufferedReader(new InputStreamReader(oConnection.getInputStream()));
        sbResponse = new StringBuilder();

        while((sLine = oReader.readLine()) != null)
        {
            sbResponse.append(sLine);
        }

        sResponse = sbResponse.toString();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    return sResponse;
}

Hope this helps!

希望这可以帮助!

#3


0  

The response of the URL will contain the xml, you just have to type cast it to a String and parse for the required data

URL的响应将包含xml,您只需将其类型转换为字符串并解析所需的数据

#1


9  

Did you try the XML SAX parser provide by Java?

您尝试过Java提供的XML SAX解析器吗?

Here is a sample code:

下面是一个示例代码:

URL url = new URL("http://www.xyz.com/book.xml");
InputStream stream = url.openStream();
Document doc = docBuilder.parse(stream);

#2


1  

This function will get you the full content from the URL :

此功能将从URL获取完整内容:

public String getURLContent(String p_sURL)
{
    URL oURL;
    URLConnection oConnection;
    BufferedReader oReader;
    String sLine;
    StringBuilder sbResponse;
    String sResponse = null;

    try
    {
        oURL = new URL(p_sURL);
        oConnection = oURL.openConnection();
        oReader = new BufferedReader(new InputStreamReader(oConnection.getInputStream()));
        sbResponse = new StringBuilder();

        while((sLine = oReader.readLine()) != null)
        {
            sbResponse.append(sLine);
        }

        sResponse = sbResponse.toString();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    return sResponse;
}

Hope this helps!

希望这可以帮助!

#3


0  

The response of the URL will contain the xml, you just have to type cast it to a String and parse for the required data

URL的响应将包含xml,您只需将其类型转换为字符串并解析所需的数据