XML parser error with JAXB

0

I'm trying to make a parser in an XML file provided by a WebService with open data from incumbent senators at Federal Senate

The XSD file can be found directly by this link . This xsd does not specify a root element, but many roots elements. And this, of course, reflects on the WebService's return XML file.

I used the java class auto-generation feature available in NetBeans. And below is my reading class:

package control;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import jaxb.Exercicio;
import jaxb.Exercicios;
import jaxb.IdentificacaoParlamentar;
import jaxb.Parlamentares;
import jaxb.ListaParlamentarEmExercicio;
import jaxb.Mandato;
import jaxb.Parlamentar;

/**
 *
 * @author muril
 */
public class ClienteSenadores {

    private static int HTTP_COD_SUCESSO = 200;

    public static void main(String[] args){
        try {
            URL url = new URL("http://legis.senado.leg.br/dadosabertos/senador/lista/atual");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            if(con.getResponseCode() != HTTP_COD_SUCESSO){
                throw new RuntimeException("HTTP erro code: " + con.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));

            JAXBContext jaxbcontext = JAXBContext.newInstance(Parlamentares.class);

            Unmarshaller jaxbUn = jaxbcontext.createUnmarshaller();

            Parlamentares parlamentares = (Parlamentares) jaxbUn.unmarshal(br);


        } catch (MalformedURLException ex) {
            Logger.getLogger(ClienteSenadores.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ClienteSenadores.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(ClienteSenadores.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

I have the following error in response:

javax.xml.bind.UnmarshalException: elemento inesperado (uri:"", local:"ListaParlamentarEmExercicio"). Os elementos esperados são <{}Exercicio>,<{}Exercicios>,<{}IdentificacaoParlamentar>,<{}Mandato>,<{}Parlamentar>,<{}Parlamentares>,<{}PrimeiraLegislaturaDoMandato>,<{}SegundaLegislaturaDoMandato>,<{}Suplente>,<{}Suplentes>,<{}Titular>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1063)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:218)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:190)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:172)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:229)
at control.ClienteSenadores.main(ClienteSenadores.java:51)
    
asked by anonymous 15.11.2018 / 17:05

1 answer

0

I think the problem is that you're using the JAXBContext.newInstance(Class... classesToBeBound) method, but you're only passing Parlamentares.class as a parameter.

Pass the other generated classes JAXBContext.newInstance(ListaParlamentarEmExercicio.class, Parlamentares.class, Parlamentar.class, etc) , or use the method JAXBContext.newInstance(String contextPath) passing the package name of the generated classes JAXBContext.newInstance("jaxb") .

The call to method jaxbUn.unmarshal(br) should return the root element of xml, in the ListaParlamentarEmExercicio case.

    
16.11.2018 / 19:00