-1

Im trying to get the values of an xm with sax parser, and when a tag its empty, like the parser instead of skip that tag save the value of the next tag, here its my parser:

public class HandlerRegistrarActa extends DefaultHandler {


    private RegistrarActaBeanReq acta = null;
    private List<Infracciones> infracciones = null;
    private Infracciones infraccion= null;

    public RegistrarActaBeanReq getActa() {
        return acta;
    }

    boolean bid_Acta = false;
    boolean bNro_Acta = false;
    boolean bLetra_Serie = false;
    boolean bInfraccion_Lugar = false;
    boolean bInfraccion_Numero = false;
    boolean bInfraccion_KmAltura = false;
    boolean bInfraccion_Referencia = false;
    boolean bInfraccion_Fec_Hora_Labrada = false;
    boolean bFec_Vencimiento = false;
    boolean bRetiene_Licencia = false;
    boolean bRetene_Vehiculo = false;
    boolean bTipoVehiculo = false;
    boolean bTipoPatente = false;
    boolean bConduccionPeligrosa = false;
    boolean bDominio = false;
    boolean bMarca = false;
    boolean bColor = false;
    boolean bSinTitular = false;
    boolean bTipo_Dni_Propietario = false;
    boolean bDni_propietario = false;
    boolean bNombre_Propietario = false;
    boolean bApellido_Propietario = false;
    boolean bRazon_Social_Propietario = false;
    boolean bCalle_propietario = false;


        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {

            if (qName.equalsIgnoreCase("id_Acta")) {
                bid_Acta = true;
                acta = new RegistrarActaBeanReq();
            } else if (qName.equalsIgnoreCase("Nro_Acta")) {
                bNro_Acta = true;
            } else if (qName.equalsIgnoreCase("Letra_Serie")) {
                bLetra_Serie = true;
            } else if (qName.equalsIgnoreCase("Infraccion_Lugar")) {
                bInfraccion_Lugar = true;
            } else if (qName.equalsIgnoreCase("Infraccion_Numero_Ruta")) {
                bInfraccion_Numero = true;
            } else if (qName.equalsIgnoreCase("Infraccion_KmAltura")) {
                bInfraccion_KmAltura = true;
            } else if (qName.equalsIgnoreCase("Infraccion_Referencia")) {
                bInfraccion_Referencia = true;
            } else if (qName.equalsIgnoreCase("Fec_Hora_Labrada")) {
                bInfraccion_Fec_Hora_Labrada = true;
            } else if (qName.equalsIgnoreCase("Fec_Vencimiento")) {
                bFec_Vencimiento = true;
            } else if (qName.equalsIgnoreCase("Retiene_Licencia")) {
                bRetiene_Licencia = true;
            } else if (qName.equalsIgnoreCase("Retiene_Vehiculo")) {
                bRetene_Vehiculo = true;
            } else if (qName.equalsIgnoreCase("TipoVehiculo")) {
                bTipoVehiculo = true;
            } else if (qName.equalsIgnoreCase("TipoPatente")) {
                bTipoPatente = true;
            } else if (qName.equalsIgnoreCase("ConduccionPeligrosa")) {
                bConduccionPeligrosa = true;
            } else if (qName.equalsIgnoreCase("Dominio")) {
                bDominio= true;
            } else if (qName.equalsIgnoreCase("Marca")) {
                bMarca = true;
            } else if (qName.equalsIgnoreCase("Color")) {
                bColor = true;
            } else if (qName.equalsIgnoreCase("SinTitular")) {
                bSinTitular = true;
            } else if (qName.equalsIgnoreCase("Tipo_Dni_Propietario")) {
                bTipo_Dni_Propietario = true;
            } else if (qName.equalsIgnoreCase("Dni_propietario")) {
                bDni_propietario = true;
            } else if (qName.equalsIgnoreCase("Nombre_Propietario")) {
                bNombre_Propietario = true;
            } else if (qName.equalsIgnoreCase("Apellido_Propietario")) {
                bApellido_Propietario = true;
            } else if (qName.equalsIgnoreCase("Razon_Social_Propietario")) {
                bRazon_Social_Propietario = true;
            } else if (qName.equalsIgnoreCase("Calle_propietario")) {
                bCalle_propietario = true;
            } 
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {

        }

        @Override
        public void characters(char ch[], int start, int length) throws SAXException {

            if (bid_Acta) {
                acta.setId_Acta(new String(ch, start, length).trim());
                bid_Acta = false;
            } else if (bNro_Acta) {
                acta.setNro_Acta(new String(ch, start, length).trim());
                bNro_Acta = false;
            } else if (bLetra_Serie) {
                acta.setLetra_Serie(new String(ch, start, length).trim());
                bLetra_Serie = false;
            } else if (bInfraccion_Lugar) {
                acta.setInfraccion_Lugar(new String(ch, start, length).trim());
                bInfraccion_Lugar = false;
            } else if (bInfraccion_Numero) {
                acta.setInfraccion_Numero_Ruta(new String(ch, start, length).trim());
                bInfraccion_Numero = false;
            } else if (bInfraccion_KmAltura) {
                acta.setInfraccion_KmAltura(new String(ch, start, length).trim());
                bInfraccion_KmAltura = false;
            }else if (bInfraccion_Referencia) {
                acta.setInfraccion_Referencia(new String(ch, start, length).trim());
                bInfraccion_Referencia = false;
            } else if (bInfraccion_Fec_Hora_Labrada) {
                acta.setFec_Hora_Labrada(new String(ch, start, length).trim());
                bInfraccion_Fec_Hora_Labrada = false;
            } else if (bFec_Vencimiento) {
                acta.setFec_Vencimiento(new String(ch, start, length).trim());
                bFec_Vencimiento = false;
            } else if (bRetiene_Licencia) {
                acta.setRetiene_Licencia(new String(ch, start, length).trim());
                bRetiene_Licencia = false;
            } else if (bRetene_Vehiculo) {
                acta.setRetene_vehiculo(new String(ch, start, length).trim());
                bRetene_Vehiculo = false;
            }else if (bTipoVehiculo) {
                acta.setTipo_Vehiculo(new String(ch, start, length).trim());
                bTipoVehiculo = false;
            } else if (bTipoPatente) {
                acta.setTipo_Patente(new String(ch, start, length).trim());
                bTipoPatente = false;
            } else if (bConduccionPeligrosa) {
                acta.setConduccion_Peligrosa(new String(ch, start, length).trim());
                bConduccionPeligrosa = false;
            } else if (bDominio) {
                acta.setDominio(new String(ch, start, length).trim());
                bDominio = false;
            } else if (bMarca) {
                acta.setMarca(new String(ch, start, length).trim());
                bMarca = false;
            }else if (bColor) {
                acta.setColor(new String(ch, start, length).trim());
                bColor = false;
            } else if (bSinTitular) {
                acta.setSinTitular(new String(ch, start, length).trim());
                bSinTitular = false;
            } else if (bTipo_Dni_Propietario) {
                acta.setTipo_Dni_Propietartio(new String(ch, start, length).trim());
                bTipo_Dni_Propietario = false;
            } else if (bDni_propietario) {
                acta.setDni_Propietario(new String(ch, start, length).trim());
                bDni_propietario = false;
            } else if (bNombre_Propietario) {
                acta.setNombre_Propietario(new String(ch, start, length).trim());
                bNombre_Propietario = false;
            }else if (bApellido_Propietario) {
                acta.setApellido_Propietario(new String(ch, start, length).trim());
                bApellido_Propietario = false;
            } else if (bRazon_Social_Propietario) {
                acta.setRazon_Social_Propietario(new String(ch, start, length).trim());
                bRazon_Social_Propietario = false;
            } else if (bCalle_propietario) {
                acta.setCalle_Propietario(new String(ch, start, length).trim());
                bCalle_propietario = false;
            } 
        }
    }

the xml im trying to get the values its:

<SDTActaCaminera xmlns='VialMza'><Acta>
<Id_Acta>1</Id_Acta>
<Nro_Acta>K0000000001</Nro_Acta>
<Letra_Serie>989</Letra_Serie>
<Infraccion_Lugar>RUTA NACIONAL</Infraccion_Lugar>
<Infraccion_Numero_Ruta>142</Infraccion_Numero_Ruta>
<Infraccion_KmAltura>10</Infraccion_KmAltura>
<Infraccion_Referencia>Aaa</Infraccion_Referencia>
<Fec_Hora_Labrada>20/02/2018 11:55:30</Fec_Hora_Labrada>
<Fec_Vencimiento>07/03/2018</Fec_Vencimiento>
<Retiene_Licencia>N</Retiene_Licencia>
<Retiene_Vehiculo>S</Retiene_Vehiculo>
<TipoVehiculo>AUTOMOVIL</TipoVehiculo>
<TipoPatente>AGF310</TipoPatente>
<ConduccionPeligrosa>S</ConduccionPeligrosa>
<Dominio>AGF310</Dominio>
<Marca>FIAT</Marca>
<Color>ROJO</Color>
<SinTitular>N</SinTitular>
<Tipo_Dni_Propietario>DNI</Tipo_Dni_Propietario>
<Dni_Propietario>31156986</Dni_Propietario>
<Nombre_Propietario>ANDRES</Nombre_Propietario>
<Apellido_Propietario>MARCOS</Apellido_Propietario>
<Razon_Social_Propietario></Razon_Social_Propietario>
<Calle_Propietario>AAA</Calle_Propietario>
</Acta>
</SDTActaCaminera>

as you can see tag razon_social_propietario its empty and instead of skip that tag it save the next tag value(in this case AAA)

  • 1
    Please reduce this to a [mcve] - there's no need for us to wade through nearly 500 lines of code. But when you reduce it, it would be very helpful if you'd make it *complete* as well - make it so we can just copy, compile, and run, with a main method etc. – Jon Skeet Feb 20 '18 at 19:27
  • I just reduce the code untill the tag im having the problem – Gaston de la cruz Feb 20 '18 at 19:35
  • It's still nearly 200 lines of code, and incomplete. You should reduce it to *just* what's required to reproduce the problem, including reducing the XML file as far as possible too. – Jon Skeet Feb 20 '18 at 19:36
  • 2
    (Fundamentally your approach of having all those booleans seems problematic to me though. Why not just keep the last-encountered element name?) – Jon Skeet Feb 20 '18 at 19:37
  • Additionally, I'd suggest adding diagnostics logging when each of the methods is being called and with what values. – Jon Skeet Feb 20 '18 at 19:38
  • If you use booleans like this, unset them in the end tag, not on the next text. Because there will be no text if the tag is empty. – Johannes Kuhn Feb 20 '18 at 19:40
  • thanks a lot @JohannesKuhn i putted the value in false on tags end and its working – Gaston de la cruz Feb 20 '18 at 20:01

2 Answers2

1

Of course your code is doing that.

When you hit a empty tag, startElement and endElement is called, but characters is not, so when the next element hits, you end up with two flags set.

I would suggest you get rid of all the flags, and remember the active tag name instead, then clear that name in endElement. You then do the multi-way name check in characters and assign as necessary. Will greatly simplify your code too.

private RegistrarActaBeanReq acta = new RegistrarActaBeanReq();
private String activeName;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    activeName = qName;
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    activeName = null;
}

@Override
public void characters(char ch[], int start, int length) throws SAXException {
    if (activeName == null)
        return;
    if (activeName.equalsIgnoreCase("id_Acta"))
        acta.setId_Acta(new String(ch, start, length).trim());
    else if (qName.equalsIgnoreCase("Nro_Acta"))
        acta.setNro_Acta(new String(ch, start, length).trim());
    else if ...
        ...
}

I would however suggest you use a StAX parser instead. Much easier to use.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

The problem can be broken down to:

public class HandlerRegistrarActa extends DefaultHandler {
    private RegistrarActaBeanReq acta = null;
    private boolean bRazon_Social_Propietario = false;
    private boolean bCalle_propietario = false;

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("id_Acta")) {
            bid_Acta = true;
            acta = new RegistrarActaBeanReq();
        } else if (qName.equalsIgnoreCase("Razon_Social_Propietario")) {
            bRazon_Social_Propietario = true;
        } else if (qName.equalsIgnoreCase("Calle_propietario")) {
            bCalle_propietario = true;
        } 
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

    }

    public void characters(char ch[], int start, int length) throws SAXException {

        if (bRazon_Social_Propietario) {
            acta.setRazon_Social_Propietario(new String(ch, start, length).trim());
            bRazon_Social_Propietario = false;
        } else if (bCalle_propietario) {
            acta.setCalle_Propietario(new String(ch, start, length).trim());
            bCalle_propietario = false;
        } 
    }
}

with the following XML:

<SDTActaCaminera xmlns='VialMza'>
    <Acta>
         <Razon_Social_Propietario></Razon_Social_Propietario>
         <Calle_Propietario>AAA</Calle_Propietario>
    </Acta>
</SDTActaCaminera>

This is your Minimal, Complete, and Verifiable example (or short MCVE).

So, what happens if there is an empty tag?
The characters method is not called for that element.
So bRazon_Social_Propietario will stay true. And the next time characters is called, it will use that as value for Razon_Social_Propietario.
But it is not longer in that tag, but in the next tag.

To fix that, you have to unset the booleans in endElement:

public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.equalsIgnoreCase("Razon_Social_Propietario")) {
        bRazon_Social_Propietario = false;
    } else if (qName.equalsIgnoreCase("Calle_propietario")) {
        bCalle_propietario = false;
    }
}

I leave it to you to implement this for all properties.

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73