Vikas Gaurav
Vikas Gaurav

Reputation: 254

How to remove white space from characters() method of DefaultHandler?

I am new for SAXParser.I am trying to read the element from XML file using characters() method of DefaultHandler class.The element is coming but vertical whitespace is coming.

Here  is my code:-
student.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <students>
        <student>
            <sno>10</sno>
            <sname>Vikas</sname>
        </student>
    </students>

here is Student.java:-
package com.vikubabu;

public class Student {
    private Integer sno;
    private String sname;

    public Integer getSno() {
        return sno;
    }

    public void setSno(Integer sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "Student [sno=" + sno + ", sname=" + sname + "]";
    }

}

Here is my MyHandler class which is extending DefaultHandler class. MyHandler.java:-

    package com.vikubabu;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyHandler extends DefaultHandler {
    StringBuilder builder;
    @Override
    public void startDocument() throws SAXException {
        //System.out.println();
         builder=new StringBuilder();

    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
    }
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
    }
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
    String tag= new String(ch,start,length);
        builder.append(tag);
        System.out.println(tag);
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        // TODO Auto-generated method stub
        super.ignorableWhitespace(ch, start, length);
    }

}


and here finally,

App.java which containing the main method:-

    package com.vikubabu;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.ParserFactory;

public class App {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setIgnoringElementContentWhitespace(Boolean.TRUE);
        saxParserFactory.setNamespaceAware(Boolean.TRUE);
        SAXParser saxParser = saxParserFactory.newSAXParser();
        MyHandler myHandler=new MyHandler();
        saxParser.parse("student.xml", myHandler);

    }

}

Upvotes: 0

Views: 923

Answers (1)

Nishesh Pratap Singh
Nishesh Pratap Singh

Reputation: 2181

You are getting white spaces because they are present in your XML. characters function will read each and every character in XML except for the tags. As you are not applying any kind of checking while appending characters in builder all white spaces are getting printed. You can verify that by replacing all white spaces in your XML, then you will not get any in your out put.

Or you can try below changes for your current XML :

public void characters(char[] ch, int start, int length) throws SAXException {
    String tag= new String(ch,start,length);        
    if(tag.trim().length()>0){
        builder.append(tag);
        System.out.println(tag);    
    }
}

Upvotes: 1

Related Questions