Collect data from xml online

1

I explain a little what I want to do and what I have tried. I'm doing a java project with JSP from a betting page. So, I have an api of resultados-futbol.com that gives me all the soccer matches. This is the xml format.

So my idea is to collect the data, and put it in a database. I have tried things, but everything was with xml in the computer itself. I have not managed to extract anything with remote xml. Any idea or some piece of code to get an idea?

package cargarXml;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;    
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.Document;         // |
import org.jdom2.Element;          // |\ Librerías
import org.jdom2.JDOMException;    // |/ JDOM
import org.jdom2.input.SAXBuilder; // |
import org.xml.sax.SAXException;



/**
 *
 * @author imanol
 */
public class CargarXml {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws MalformedURLException, IOException,                 ParserConfigurationException, SAXException {
        //Se crea un SAXBuilder para poder parsear el archivo
//    SAXBuilder builder = new SAXBuilder();
//    File xmlFile = new File( "http://apiclient.resultados-    futbol.com/scripts/api/api.php?    key=XXXXXXXXXXXXXXXXXXXXXXX&tz=Europe/Madrid&format=xml&req=matchs&league=1&    round=38&order=twin&twolegged=1&year=2017" );
URL url = new URL("http://apiclient.resultados-futbol.com/scripts/api/api.php?



     key=XXXXXXXXXXXXXXXXXXXXXXX&tz=Europe/Madrid&format=xml&req=matchs&league=1&
round=38&order=twin&twolegged=1&year=2017");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());




try
{
    //Se crea el documento a traves del archivo
    //Document document = (Document) builder.build( xmlFile );

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = (Document) dBuilder.parse( in );

    //Se obtiene la raiz 'tables'
    Element rootNode = doc.getRootElement();

    //Se obtiene la lista de hijos de la raiz 'tables'
    List list = rootNode.getChildren( "matchs" );

    //Se recorre la lista de hijos de 'tables'
    for ( int i = 0; i < list.size(); i++ )
    {
        //Se obtiene el elemento 'tabla'
        Element tabla = (Element) list.get(i);

        //Se obtiene el atributo 'nombre' que esta en el tag 'tabla'
        //String nombreTabla = tabla.getAttributeValue("nombre");

        System.out.println( "Tabla: " );

        //Se obtiene la lista de hijos del tag 'tabla'
        List lista_campos = tabla.getChildren();

        System.out.println( "\tNombre\t\tTipo\t\tValor" );

        //Se recorre la lista de campos
        for ( int j = 0; j < lista_campos.size(); j++ )
        {
            //Se obtiene el elemento 'campo'
            Element campo = (Element)lista_campos.get( j );

            //Se obtienen los valores que estan entre los tags '<campo></campo>'
            //Se obtiene el valor que esta entre los tags '<nombre></nombre>'
            String nombre = campo.getChildTextTrim("id");

            //Se obtiene el valor que esta entre los tags '<tipo></tipo>'
            String tipo = campo.getChildTextTrim("year");

            //Se obtiene el valor que esta entre los tags '<valor></valor>'
            String valor = campo.getChildTextTrim("group");

            System.out.println( "\t"+nombre+"\t\t"+tipo+"\t\t"+valor);
        }
    }
}catch ( IOException io ) {
    System.out.println( io.getMessage() );
}
}

}

Code that I tried, and the error:

  

Exception in thread "main" java.lang.ClassCastException:   com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl can not be   cast to org.jdom2.Document at   UploadXml.CargarXml.main (UploadXml.java:49)   C: \ Users \ imanol.OFFICIENT \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53:   Java returned: 1 BUILD FAILED (total time: 1 second)

    
asked by urrutias 22.05.2017 в 11:48
source

1 answer

1

I think you've imported the wrong library. Instead of:

import org.jdom2.Document;         // |
import org.jdom2.Element;          // |\ Librerías
import org.jdom2.JDOMException;    // |/ JDOM
import org.jdom2.input.SAXBuilder; 

you should use:

import org.w3c.dom.Document;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

And to read the document:

URL url = new URL("http://apiclient.resultados-futbol.com/scripts   /api/api.php?
 key=XXXXXXXXXXXXXXXXXXXXXXX&tz=Europe/Madrid&format=xml&req=matchs&league=1&round=38&order=twin&twolegged=1&year=2017");
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = (Document) db.parse(in);  
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("match");
for (int temp = 0; temp < nList.getLength(); temp++) {

    Node nNode = nList.item(temp);

    System.out.println("\nCurrent Element :" + nNode.getNodeName());

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;

        System.out.println("Team1 : " + eElement.getElementsByTagName("team1").item(0).getTextContent());
        System.out.println("Team2 : " + eElement.getElementsByTagName("team2").item(0).getTextContent());


    }
}
} catch (Exception e) {
    e.printStackTrace();
}
    
answered by 22.05.2017 / 13:21
source