XML não lê

0 respostas
W

Boa noite a todos

Estou dando andamento a um projeto que recebe arquivos exportados de uma plataforma c#, porém não sei se meu código está correto, o problema é que não da erro somente não aparece nada, segue abaixo o código.
package Celular;
import javax.microedition.lcdui.Canvas.*;



import java.io.*;
import java.io.InputStream.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;


 public final class Onis extends MIDlet implements CommandListener
{

    private Display tela;
    private static final Command EXIT_COMMAND = new Command("Sair", Command.EXIT, 0);
    private static final Command OK_COMMAND = new Command("OK", Command.OK, 0);
    private DesenhaTabela tabela,aux;
    protected boolean started;
    private boolean STOPED=false;
    private Thread pausa;
    private TextBox t;
   
    public void startApp()
    {
        int x,y;
        x = 0;
        y = 0;

         if (!started)
         {
            started = true;
            tela = Display.getDisplay(this);

            tabela = new DesenhaTabela();

            pausa = new Thread();

            tabela.addCommand(OK_COMMAND);
            tabela.addCommand(EXIT_COMMAND);
            tabela.setCommandListener(this);


            tela.setCurrent(tabela);
            try {
                rodarprancha();
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    }
    public void commandAction(Command c, Displayable d)
    {
        if (c == EXIT_COMMAND) {
         destroyApp(true);
         notifyDestroyed();
      } else if (c == OK_COMMAND) {
         
              STOPED=true;
        }
    }

    

    private void rodarprancha() throws Exception
    {
        int x,y, tempmusica,indice;
        long t = 0;
        String tempo = null;
        String[][] dadosxml = new String[9][3];

        x=y=indice=0;
        tabela.setimage();
        dadosxml= tabela.getDadosXml();

        while(true)
        {
            while(true)
            {
                if(indice==9)
                    indice=0;
                try {tabela.setimage();}
                catch (Exception ex) {
                    ex.printStackTrace();
                }

                tabela.setBorda(x,y);
                tabela.setTextCel(dadosxml[indice][0]);
                
                if(STOPED==true)
                {
                    STOPED = false;
                    if((indice-1)<(0))
                        tocarMusica(dadosxml[8][1]);
                    else
                        tocarMusica(dadosxml[indice-1][1]);
                    
                    try {
                        System.out.println("valor tempo:"+tempo);
                        pausa.sleep(2500);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
               
                tabela.repaint();
                try {pausa.sleep(100);
                }catch (InterruptedException ex){}
                indice++;

                if(x == 2)
                    {x=0;break;}
                x++;              
            }
             y++;

            if (y == 3)
                {y=0;x=0;}
         }
    }
    private void tocarMusica(String nomemusica)
    {      
        //long tempo;
        try
        {
            Player player;
            VolumeControl vc;
            InputStream is = getClass().getResourceAsStream("/Sons/"+nomemusica+".mp3");
            player = Manager.createPlayer(is, "audio/mpeg");
            player.realize();            
            
            vc = (VolumeControl) player.getControl("VolumeControl");
            if (vc == null)
            {
                vc.setLevel(100);
            }

            player.prefetch();
            //tempo = player.getDuration();
            player.start();
            //return tempo;
        }
        catch (Exception e)
        { e.printStackTrace();
        //return 0;
        }
    }
    protected void pauseApp() {

    }

    protected void destroyApp(boolean unconditional)
    {

    }

    


}
 


class DesenhaTabela extends Canvas
{
    
    KXmlParser leitor = new KXmlParser();
    
    private Graphics a;
    private Image imagem=null;
    private Image imgdir=null;
    private Image img =null;
    private Image pranchasemborda=null;
    private int width = getWidth();
    private int height = getHeight();
    boolean pranchacriada = false;
    

    //clCelula[][] dadosxml=new clCelula[4][4];
    String[][] dadosxml = new String[9][3];

    public void paint(Graphics g)
    {
//        if(pranchacriada==false)
//        {
//            try {
//                imagem = criarprancha();
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }
        g.drawImage(imagem, 0, 0, Graphics.TOP|Graphics.LEFT);
    }
    private Image pegarimagem(String NomeImagem)
    {
        Image img=null;

        try
        {
        img = Image.createImage("/Imagens/"+NomeImagem+".JPG");
            return img;
        }
        catch (IOException ex)
        {
            a.setColor(0xffffff);
            a.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
            return null;
        }
    }
    private String[] parser() throws Exception
    {
        int indice=0;
        //Inicia o XMLParser
        String[] cel= new String[4];
        while(leitor.nextTag()!=KXmlParser.END_TAG)
        {
            leitor.require(XmlPullParser.START_TAG, null, "Prancha");
            String name = leitor.getName();
            String text = leitor.nextText();
            cel[indice]=text;
            
            leitor.require(XmlPullParser.END_TAG, null, "Celula");
            indice++;
        }
        return cel;
    }
      
    public Image criarprancha() throws Exception
    {
        
        int x=0;
        int auxx=0;
        int y=1;
        int indice=0;

        Image aux=null, img=null;
        String[] celula = new String[4];

        img = Image.createImage(width, width+31);

        a = img.getGraphics();
        a= colocaLinha(a);
        setFundoTitulo();

        InputStream in = getClass().getResourceAsStream("celulas.xml");
        leitor.setInput(new InputStreamReader(in));
        leitor.nextTag();
        //Posiciona na tag <Prancha>
        leitor.require(XmlPullParser.START_TAG, null, "Prancha");

        while(leitor.nextTag()!=XmlPullParser.END_TAG)
        {
            x++;
               
            //Posiciona na tag <Celula>
            leitor.require(XmlPullParser.START_TAG, null, "Celula");

            celula=parser();
            dadosxml[indice][0]=celula[0];
            dadosxml[indice][1]=celula[1];
            dadosxml[indice][2]=celula[2];
            
            aux=null;
            aux = pegarimagem(celula[2]);

            aux = createThumbnail(aux,((width/3)-1),((width/3)-1));
            a.drawImage(aux, ((x*(width/3))-(width/6)), ((y*(width/3))-(width/6))+20, Graphics.VCENTER|Graphics.HCENTER);
            leitor.require(XmlPullParser.END_TAG, null, "Celula");

            if(x==3)
            {
                y++;
                x=0;
            }
            indice++;
            if(indice==9)
                indice=0;
           
         }
        
        leitor.require(XmlPullParser.END_TAG, null, "Prancha");
        leitor.next();
        leitor.require(XmlPullParser.END_DOCUMENT,  null, null);
            
        return img;
    }
    
    public String[][] getDadosXml()
    {
        return dadosxml;
    }


    public void setFundoTitulo()
    {
        a.setColor(0,245,255);
        a.fillRect(0, 0, getWidth(), 20);
        a.setColor(0,0,156);
        a.drawRect(0, 0, getWidth(), 20);


//        a.setColor(0,245,255);
//        a.fillRect(0, width+15, getWidth(), 15);
//        a.setColor(0,0,156);
//        a.drawRect(0, width+15, getWidth(), 15);
    }
    public void setTextCel(String texto)
    {
        a.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL));
        a.drawString("Prancha - "+texto, (width/2),1, Graphics.HCENTER | Graphics.TOP);
    }

    private Graphics colocaLinha(Graphics g)
    {
        g.setColor(0,0,156);
        g.drawLine((width/3), 20, (width/3),width+20);
        g.drawLine(2*(width/3), 20, 2*(width/3),width+20);
        g.drawLine(0,(width/3)+20, width,(width/3)+20);
        g.drawLine(0,2*(width/3)+20, width,2*(width/3)+20);
        g.drawLine(0, width+20, width, width+20);
        g.drawLine(0,0,0,width+20);
        g.drawLine(width,0,width,width+20);

        return g;
    }
    private Image createThumbnail(Image image, int thumbWidth, int thumbHeight)
    {

            int dx, dy;
            int imageWidth = image.getWidth();
            int imageHeight = image.getHeight();


            if (imageHeight >= imageWidth)
                thumbWidth = (imageWidth * (thumbHeight))/ imageHeight;
            if (imageWidth >= imageHeight)
                thumbHeight = (imageHeight * (thumbWidth)) / imageWidth;

            Image thumb = Image.createImage(thumbWidth,thumbHeight);

            Graphics g = thumb.getGraphics();

            for (int y = 0; y < (thumbHeight); y++)
            {
                for (int x = 0; x < (thumbWidth); x++)
                {

                    g.setClip(x, y, 1, 1);
                    dx = x * imageWidth / thumbWidth;
                    dy = y * imageHeight / thumbHeight;

                    g.drawImage(image, x-dx,y-dy  , Graphics.LEFT|Graphics.TOP);
                }
            }
            return thumb;
    }

    public void setBorda(int x, int y)
    {
        Graphics z;

        z = imagem.getGraphics();
        z.setColor(0,245,255);
        z.setClip(x*(width/3)+1,y*(width/3)+21,(width/3),(width/3));
        z.drawRect(x*(width/3)+1,y*(width/3)+21 , (width/3)-2, (width/3)-2);
        z.drawRect(1+(x*(width/3)+1), 1+(y*(width/3))+21, (width/3)-4, (width/3)-4);
        z.drawRect(2+(x*(width/3))+1,2+(y*(width/3))+21, (width/3)-6, (width/3)-6);

    }

    public void setimage() throws Exception
    {
     
            imagem = criarprancha();
        
    }
}
este classe java lê o xml abaixo:
Prancha>
	<Celula>
		<Texto>SIMA</Texto>
		<Som>SIMA</Som>
		<Imagem>SOLA</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Beber Agua</Texto>
		<Som>SIME</Som>
		<Imagem>SOLE</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Estou Bem</Texto>
		<Som>SIMI</Som>
		<Imagem>SOLI</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Estou com fome</Texto>
		<Som>SIMO</Som>
		<Imagem>SOLO</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Feliz</Texto>
		<Som>SIMU</Som>
		<Imagem>SOLU</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Não</Texto>
		<Som>SIMAA</Som>
		<Imagem>SOLAA</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Obrigado</Texto>
		<Som>SIMEE</Som>
		<Imagem>SOLEE</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Sim</Texto>
		<Som>SIMII</Som>
		<Imagem>SOLII</Imagem>
		<Link></Link>
	</Celula>
	<Celula>
		<Texto>Triste</Texto>
		<Som>SIMOO</Som>
		<Imagem>SOLOO</Imagem>
		<Link></Link>
	</Celula>
</Prancha>

a pasta imagens e sons estão dentro do jar juntamente com o xml.
gostaria de saber se meu código está correto, pois é a primeira vez que trabalho com xml.

Por favor me ajudem ai, não sei se fui muito claro, se precisarem de mais informaçãoes postem ai blz?

desde ja agradeço.

Criado 18 de outubro de 2011
Respostas 0
Participantes 1