Skip to main content
5 de 5
replaced http://stackoverflow.com/ with https://stackoverflow.com/

Sax parser xml string Java Android

Gostaria de uma ajuda sobre um problema que estou a ter em Java Android.

Primeiramente leio uma String XML para obter os valores de cada tag. Ao obter os valores lê bem as tags, só que de 9 em 9 Strings recebidas lê em partes, se alguém souber como resolver agradeço pela ajuda.

Em seguida apresento o código que uso para ler o XML.

SAXParser saxParser = saxParserFactory.newSAXParser();
Parse_xml parseXMLClass = new Parse_xml();
saxParser.parse(new InputSource(new StringReader(message_posted)), parseXMLClass);

A seguir é class que faz a leitura das tags:

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

if (localName.equalsIgnoreCase("MSG"))
    {
        if (listMSG == null)
        {
            listMSG = new ArrayList<MSG>();

        }
    }

    if (localName.equalsIgnoreCase("Delete"))
    {
        if (listMSG == null)
        {
            listMSG = new ArrayList<MSG>();

        }
    }

    if (localName.equalsIgnoreCase("Clear"))
    {
        if (listMSG == null)
        {
            listMSG = new ArrayList<MSG>();

        }
    }

    if (localName.equalsIgnoreCase("ID"))
    {
        bID = true;
    }
    if (localName.equalsIgnoreCase("DESTINATION"))
    {
        bDestination = true;
    }
    if (localName.equalsIgnoreCase("SOURCE"))
    {
        bSource = true;
    }
    if (localName.equalsIgnoreCase("DATE"))
    {
        bDate = true;
    }
    if (localName.equalsIgnoreCase("SUBJECT"))
    {
        bSubject = true;
    }
    if (localName.equalsIgnoreCase("BODY"))
    {
        bMessage = true;
    }
    if (localName.equalsIgnoreCase("SMS"))
    {
        bType = true;
    }
    if (localName.equalsIgnoreCase("TTL"))
    {
        config = new Configuration();
        bTtl = true;
    }
    if (localName.equalsIgnoreCase("ATTEMPTS"))
    {
        bAttempet = true;
    }
    
    if (localName.equalsIgnoreCase("SMTP"))
    {
        bSmtp = true;
    }
    if (localName.equalsIgnoreCase("PORT"))
    {
        bPort = true;
    }
    if (localName.equalsIgnoreCase("USERNAME"))
    {
        bUsername = true;
    }
    if (localName.equalsIgnoreCase("PASSWORD"))
    {
        bPass = true;
    }
    if (localName.equalsIgnoreCase("EMAIL"))
    {
        bEmail = true;
    }
    if (localName.equalsIgnoreCase("STATUS"))
    {
        bStatus = true;
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException
{
    if (localName.equalsIgnoreCase("MSG"))
    {
        listMSG.add(msg);
        msg = new MSG();
    }
    if (localName.equalsIgnoreCase("DELETE"))
    {
        listMSG.add(msg);
        msg = new MSG();
    }
    if (localName.equalsIgnoreCase("Clear"))
    {
        listMSG.add(msg);
        msg = new MSG();
    }

}

@Override
public void characters(char ch[], int start, int length)
        throws SAXException
{
     */
    if (bID)
    {
        String id=String.copyValueOf(ch, start, length).trim();
        msg.setId(Integer.parseInt(id));
        bID = false;
    }
    if (bDestination)
    {
        String destinaton=String.copyValueOf(ch, start, length).trim();
        msg.setDestination(destinaton);
        bDestination = false;
    }
    if (bSource)
    {
        String source=String.copyValueOf(ch, start, length).trim();
        msg.setSource(source);
        bSource = false;
    }
    if (bDate)
    {
        String date=String.copyValueOf(ch, start, length).trim();
        msg.setDate(date);
        bDate = false;
    }
    if (bSubject)
    {
        String subject=String.copyValueOf(ch, start, length).trim();;
        msg.setSubject(subject);
        bSubject = false;
    }
    if (bMessage)
    {
        String body=String.copyValueOf(ch, start, length).trim();
        msg.setbody(body);
        bMessage = false;
    }
    if (bType)
    {
        String type=String.copyValueOf(ch, start, length).trim();
        msg.setType(Integer.parseInt(type));
        bType = false;
    }
    if (bStatus)
    {
        String status=String.copyValueOf(ch, start, length).trim();
        msg.setStatus(Integer.parseInt(status));
        bStatus = false;
    }
    if (bTtl)
    {
        String ttl=String.copyValueOf(ch, start, length).trim();
        config.setTtl(Integer.parseInt(ttl));
        bTtl = false;
    }
    if (bAttempet)
    {
        String attempt=String.copyValueOf(ch, start, length).trim();
        config.setAttempt(Integer.parseInt(attempt));
        bAttempet = false;
    }
    if (bSmtp)
    {
        String smtp=String.copyValueOf(ch, start, length).trim();
        config.setSmtp(smtp);
        bSmtp = false;
    }
    if (bPort)
    {
        String porta=String.copyValueOf(ch, start, length).trim();
        config.setPorta(Integer.parseInt(porta));
        bPort = false;
    }
    if (bUsername)
    {
        String username=String.copyValueOf(ch, start, length).trim();
        config.setUsername(username);
        bUsername = false;
    }
    if (bPass)
    {
        String pass=String.copyValueOf(ch, start, length).trim();
        config.setPass(pass);
        bPass = false;
    }
    if (bEmail)
    {
        String email=String.copyValueOf(ch, start, length).trim();
        config.setEmail(email);
        bEmail = false;
    }
    

}

Pergunta SOEN

Ludger
  • 315
  • 1
  • 10