How to read an XML file from the server

Asked

Viewed 685 times

2

To create a java desktop application that accesses the internet and le an xml file. Well, this is the initial proprosito. I am using the Socket class to communicate between the server and the application. But now I don’t know how to read the xml. I changed my code, no while I plan to save the XML. Still searching as.

 public static void main(String[] args) throws IOException
{
    try
    {
        //conectando
        URL url = new URL("http://www.cinemark.com.br/mobile/xml/films/");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //pegando informações
        BufferedReader leitor = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String line;
        FileWriter arquivo = new FileWriter("D:\\Documentos\\GitHub\\arquivo.txt");
        PrintWriter gravArq = new PrintWriter(arquivo);

        while ((line = leitor.readLine()) != null)
        {
            gravArq = gravArq.printf(line);
        }
        leitor.close();
        urlConnection.disconnect();


    } 
    catch (MalformedURLException ex) { System.out.println("Erro ao criar url: formato invalido"); }

}

1 answer

1

Having the URL of the XML file, for example www.seusite.com.br/arquivo.xml you can use the SAX API.

An example (not tested):

String url = "http://www.seusite.com.br/arquivo.xml";
XMLReader myReader = XMLReaderFactory.createXMLReader();
myReader.setContentHandler(handler);
myReader.parse(new InputSource(new URL(url).openStream()));

For more information read the SAX manual: https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html

  • Thanks for the help. I will read the documentation and the files I have on SAX and Dom on the way home. VLW same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.