Reading XML with bug

Asked

Viewed 77 times

-2

People here in the project where I use classes to read an XML works, the problem is that it is not loading the whole message string, what can it be? Ex: the provider has a text , and is only showing on the screen for the user only up to a certain point, I have already manually tested the same size playing on an Alert Builder, and it worked. the problem is really in reading XML, see my classes:

package br.com.insideweb.xml;

/**
 * Classe para retornar os dados do xml / xml data reader
 * 
 */

public class XmlReader {

private String rssUrl;

/**
 * Constructor
 */
public XmlReader(String rssUrl) {
    this.rssUrl = rssUrl;

}

/**
 * Pega uma lista de XML.
 * 
 * @return
 */
public List<ItemXml> getItems() throws Exception {
    // SAX parse RSS data
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    XmlParseHandler handler = new XmlParseHandler();

    saxParser.parse(rssUrl, handler);

    return handler.getItems();

    }

}

/**Classe para tratar o xml / parse handler */

public class XmlParseHandler extends DefaultHandler {

private List<ItemXml> rssItems;

// Used to reference item while parsing
private ItemXml currentItem;

// usa o id notificador
private boolean parsingId;
// Usa mensagem notificador
private boolean parsingMensagem;

public XmlParseHandler() {
    rssItems = new ArrayList<ItemXml>();
}

public List<ItemXml> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if ("notificacoes".equals(qName)) {
        currentItem = new ItemXml();
    } else if ("id".equals(qName)) {
        parsingId = true;
    } else if ("mensagem".equals(qName)) {
        parsingMensagem = true;
    }
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if ("notificacoes".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("id".equals(qName)) {
        parsingId = false;
    } else if ("mensagem".equals(qName)) {
        parsingMensagem = false;
    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    if (parsingId) {
        if (currentItem != null)
            currentItem.setId(new String(ch, start, length));
    } else if (parsingMensagem) {
        if (currentItem != null) {
            currentItem.setMensagem(new String(ch, start, length));
            parsingMensagem = false;
        }
    }
}

}

package br.com.insideweb.model;

/**Classe de entidade do xml*/

public class ItemXml {

// item id
private String id;
// item mensagem
private String mensagem;


@Override
public String toString() {
    return ""+mensagem;
}


public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}


public String getMensagem() {
    return mensagem;
}


public void setMensagem(String mensagem) {
    this.mensagem = mensagem;
}

}



/**
  * Tela para ler a notificação */

@SuppressLint("NewApi") 
public class ReaderNotificacao extends Activity {

private AlertDialog.Builder msg;
private AlertDialog alert;
@SuppressLint("NewApi") @Override
protected void onCreate(android.os.Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    // ler o xml

lerNotificacoes();
}
// Vai carregar as notificações
    public void lerNotificacoes() {
        try {
            XmlReader reader = new XmlReader("meuxml.xml");
            msg = new AlertDialog.Builder(this);
            msg.setTitle("Notificação");
            msg.setMessage(""+reader.getItems());
            msg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            });
            AlertDialog alert = msg.create();
            alert.show();
        } catch (Exception e) {

        }
    }

The problem is just that it does not load full message string.

  • I noticed that he only carries the string up to the "." point.Note on the link that use of xml, it does not make sense not to load everything, xml:http://www.loja.srv.br/sga/notifiement/fadire

  • No xml experimenta colocar <![CDATA[ Matricículas para o 2º semestre inciadas! Do not miss the deadline of your re-enrollment which is from 10/08/2014 to 15/08/2014. If you have any questions, please contact your local coordinator. Fadire Agradeçe. ]>

1 answer

0

In xml try to tag CDATA, follow example below:

<![CDATA[ Matrículas para o 2º semestre inciadas! Não perca o prazo de sua re-matrícula que é de 10/08/2014 a 15/08/2014. Em caso de dúvidas entre em contato com seu coordenador local. A Fadire agradeçe. ]]>
  • That, in case would be <message><! [CDATA[ Enrollments for the 2nd semester initiated! Do not miss the deadline of your re-enrollment which is from 10/08/2014 to 15/08/2014. If you have any questions please contact your local coordinator. Fadire Agradeçe. ]]> </message>

  • Thank you, I’ll talk to the company manager here because only he has access, soon I will return.

Browser other questions tagged

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