Sax parser xml string Java Android

Asked

Viewed 157 times

1

I’d like some help on a problem I’m having at Java Android.

First I read a String XML to get the values of each tag. When getting the values reads well the tags, only 9 in 9 Strings received reads in parts, if anyone knows how to solve thanks for the help.

Next I present the code I use to read the XML.

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

The following is class reading 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;
    }


}

Question SOEN

  • Could you post an XML demo? No formatting error? I recommend taking a look at Jackson Fasterxml, it does XML and JSON Parsing, mapping in structure Map or Object (using Reflection).

  • Thanks for the help but I got it solved. To solve had to in characters always concatenate the values of why add it in endElemnt. because when the string is long it calls the chacteres several times. Here is the answer in [link][1]. [1]: http://stackoverflow.com/a/10766976/3296571

  • Show! If you can put the solution as a response and mark as accepted, so if someone comes through this post, you will find the correct solution :)

1 answer

1

The AP was able to resolve according to response obtained in the OS.

According to the definition SAX characters() can be called several times per element. Therefore, you must accumulate the text; If this is happening, then your code will not work.

  • fortunately I have already managed to resolve and thanks for the help.

Browser other questions tagged

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