简体   繁体   中英

Sax parser xml string Java Android

Hi ,

I would like some help on a problem I'm having in Java Android .

First I read an XML String values ​​for each tag.

To get the values ​​right reads the tags, only 9 of 9 Strings received reads in parts, if anyone knows how to appreciate for the help.

Then I present the code I use to read XML .

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

Following is the class that reads the 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;
    }


}

Maybe, it helps you

@Override
    public void characters(char ch[], int start, int length)
            throws SAXException
    {
        //Your code here, and then...
        if(bYourLongType) {
           String yourLongType = String.copyValueOf(ch, start, length).trim();
           Long longValue = Long.parseLong(yourLongType);
           bYourLongType = false;
        }
    }

In sax parser, characters() method parses only maximum of 1024 characters each time. So we need to append the strings until all the characters are parsed. Check out this SO answer.

https://stackoverflow.com/a/9112055/685240

Let me know if it helps.

Please See my code

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ReadXMLFile {

   public static void main(String argv[]) {

    try {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {

    boolean bfname = false;
    boolean blname = false;
    boolean bnname = false;
    boolean bsalary = false;

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

        System.out.println("Start Element :" + qName);

        if (qName.equalsIgnoreCase("FIRSTNAME")) {
            bfname = true;
        }

        if (qName.equalsIgnoreCase("LASTNAME")) {
            blname = true;
        }

        if (qName.equalsIgnoreCase("NICKNAME")) {
            bnname = true;
        }

        if (qName.equalsIgnoreCase("SALARY")) {
            bsalary = true;
        }

    }

    public void endElement(String uri, String localName,
        String qName) throws SAXException {

        System.out.println("End Element :" + qName);

    }

    public void characters(char ch[], int start, int length) throws SAXException {

        if (bfname) {
            System.out.println("First Name : " + new String(ch, start, length));
            bfname = false;
        }

        if (blname) {
            System.out.println("Last Name : " + new String(ch, start, length));
            blname = false;
        }

        if (bnname) {
            System.out.println("Nick Name : " + new String(ch, start, length));
            bnname = false;
        }

        if (bsalary) {
            System.out.println("Salary : " + new String(ch, start, length));
            bsalary = false;
        }

    }

     };

       saxParser.parse("c:\\file.xml", handler);

     } catch (Exception e) {
       e.printStackTrace();
     }

   }

}

if you any problem please let me know..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM