XML parser error with JAXB

Asked

Viewed 156 times

0

I’m trying to parse an XML file provided by a Webservice with open data of the acting Senators on Senado Federal

The XSD file can be found directly by that link. This xsd does not specify a root element, but many Roots elements. And this of course reflects in the return XML file of Webservice.

I used the automatic java class generation feature available in the Netbeans IDE. 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)

1 answer

0


I believe the problem is that you’re using the method JAXBContext.newInstance(Class... classesToBeBound), but it’s only going through 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 the method jaxbUn.unmarshal(br) must return the xml root element, in case ListaParlamentarEmExercicio.

Browser other questions tagged

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