1

When i try to read the xml from java using SAX parser, it is unable to read the content in element present after special character

For ex:

<title>It's too difficult</title>

After reading using the SAX parser, it's displaying only It

How to handle special characters. My sample code is as below

    package com.test.java;

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 ReadXMLUsingSAXParser {




  public static void main(String argv[]) {

   try {

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

      DefaultHandler handler = new DefaultHandler() {

      int titleCount;
      boolean title = false;
      boolean description = false;

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

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


         if (qName.equalsIgnoreCase("title")) {
            title = true;
            titleCount+=1;
         }

         if (qName.equalsIgnoreCase("description")) {
            description = 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 (title&&titleCount>2) {
               System.out.println("title : "
                   + new String(ch, start, length)+":"+titleCount);
               title = false;
            }

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

         }

       };

       saxParser.parse("C:\\Documents and Settings\\sukumar\\Desktop\\sample.xml", handler);

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

 }
1

1 Answer 1

5

The characters(char ch[], int start, int length) methode does not read full lines, you should store the characters in a StringBuffer and use it in the endElemen method.

E.g.:

private StringBuffer buffer = new StringBuffer();

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

     if (qName.equalsIgnoreCase("title")) {
        System.out.println("title: " + buffer);
     }else if (qName.equalsIgnoreCase("description")) {
        System.out.println("description: " + buffer);
     }
     buffer = new StringBuffer();
}

public void characters(char ch[], int start, int length)
      throws SAXException {
     buffer.append(new String(ch, start, length));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.