Java - read XML from a URL

0

I have a function that reads XML and it works 100%, the problem is that when I try to read an XML that is online on my server I can not.

Follow the code below:

public int lerXml() throws JDOMException, IOException, URISyntaxException {
    URL url = new URL("http://br728.teste.website/~ayzac296/smh/xml.xml");
    File f = new File(url.toURI());
    SAXBuilder sb = new SAXBuilder();
    Document d;
    try {
        d = sb.build(f);
    } catch (Exception e) {
        System.out.println(e);
        return 0;
    }
    Element mural = d.getRootElement();
    List elements = mural.getChildren();
    Iterator i = elements.iterator();
    while (i.hasNext()) {
        Element element = (Element) i.next();
        if (Double.parseDouble(element.getChildText("tempo")) >= mediaPlayer.getCurrentTime().toSeconds()) {
            //System.out.println("Nome:" + element.getChildText("nome"));
            //System.out.println("Tempo:" + element.getChildText("tempo"));
            System.out.println("Menssagem:" + element.getChildText("conteudo"));
        }
    }

    return 1;
}

It is showing the following error:

  

Exception in thread "Thread-179" java.lang.IllegalArgumentException: URI scheme is not "file"

I'm having trouble turning the URL into File.

Thank you.

    
asked by anonymous 23.05.2017 / 08:52

1 answer

1

You can simply pass your URL to SAXBuilder . So:

d = sb.build(url);
    
23.05.2017 / 14:30