How to read an XML file from the server

2

To create a java desktop application that accesses the internet and le an xml file. Well, that's the initial propolis. I am using the Socket class to do communication between the server and the application. But now I do not know how to read XML. I changed my code, while I want to write the XML. Still looking like.

 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"); }

}
    
asked by anonymous 02.06.2015 / 17:36

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: link

    
02.06.2015 / 18:47