如何从DefaultHandler的characters()方法中删除空格?

时间:2017-05-07 03:55:16

标签: java xml web-services saxparser

我是SAXParser的新手。我正在尝试使用DefaultHandler类的characters()方法从XML文件中读取元素。元素即将到来,但是垂直空白即将到来。

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 + "]";
    }

}

这是我的MyHandler类,它正在扩展DefaultHandler类。     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);

    }

}

1 个答案:

答案 0 :(得分:1)

您正在获取空白区域,因为它们存在于您的XML中。 字符函数将读取XML中除标签之外的每个字符。由于您在构建器中附加字符时未应用任何类型的检查,因此将打印所有空格。您可以通过替换XML中的所有空格来验证这一点,那么您的输出就不会得到任何空白。

或者您可以尝试以下对当前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);    
    }
}