0

I have an xml as shown in the image below.

<erros>
            <error id="30" message="VENDEDOR NAO EXISTE" />
            <error id="31" message="SEM LOTERIAS" />
            <error id="37" message="TERMINAL INVALIDO"/>
            <error id="41" message="ERRO AO GRAVAR POULE"/>
            <error id="42" message="LOTERIA COM ERRO"/>
            <error id="43" message="JOGO COM ERRO"/>
            <error id="44" message="NUMERO PROIBIDO"/>
            <error id="45" message="VALE COM ERRO"/>
            <error id="46" message="LOTERIA INVALIDA"/>
            <error id="47" message="DATA INVALIDA"/>
            <error id="48" message="NUMERO COTADO"/>
            <error id="49" message="LIMITE EXCEDIDO"/>
            <error id="51" message="POULE INEXISTENTE"/>
            <error id="61" message="TERMINAL INVALIDO"/>
            <error id="62" message="COBRADOR INVALIDO"/>
            <error id="71" message="RESULTADO NAO EXISTE"/>
            <error id="81" message="SEM PREMIACAO"/>
            <error id="82" message="PULE JA PAGA"/>
            <error id="83" message="RESUMO NAO EXISTE"/>
        </erros>

Then, I would like to add a switch, comparing a variable of the whole type, with the id inside the xml, and if it has the id equal to one declared in the tags, print the message on the screen. And if not, print on the SUCCESS screen. Thk's for any help.

EXAMPLE

int i = Integer.parseInt(verifyMsg);
            switch (i) {
                case 1:
                    // Send message id 1
                    break;

                case 2:
                    // Send message id 2
                    break;
            }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    Please focus on one topic per question. For parsing xml, choose an xml parser and learn to use it, see for example: https://stackoverflow.com/questions/7373567/how-to-read-and-write-xml-files – Hulk Aug 31 '20 at 14:22
  • For checking, I'd probably populate a `Map` with the values read from the xml instead of a switch. A switch only works with compile time constants. – Hulk Aug 31 '20 at 14:25

1 Answers1

0

Use JAXB to read xml into java object as such - https://howtodoinjava.com/jaxb/read-xml-to-java-object/, and you may implement your comparison algorithm. Although, I wouldn't recommend you to use giant switch, but use map with required values and keys is ids, so you'll be able to fetch a variable from it by id.