I have a function for parsing XML in which I'm getting white spaces when event type is Text. Code is explain below in which when event type is end tag at that time white space is added to database .
My code as follows :
try {
xmlPullParserFactory = XmlPullParserFactory.newInstance();
xmlPullParserFactory.setNamespaceAware(true);
xmlPullParser = xmlPullParserFactory.newPullParser();
xmlPullParser.setInput(new InputStreamReader( new FileInputStream(read_file)));
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
tagname=xmlPullParser.getName();
if(eventType==XmlPullParser.START_TAG)
{
// System.out.println("start tag"+tagname);
if(tagname.equalsIgnoreCase("option"))
{
id=xmlPullParser.getAttributeValue("", "id");
}
else
{
id=xmlPullParser.getAttributeValue("", "id");
desc=xmlPullParser.getAttributeValue("", "description");
input_type=xmlPullParser.getAttributeValue("", "input_type");
}
}
else if(eventType==XmlPullParser.TEXT)
{
text=xmlPullParser.getText();
/// System.out.println("start tag"+tagname+" text"+text);
}
else if(eventType==XmlPullParser.END_TAG)
{
if(tagname!=null)
{
//System.out.println("end tag"+tagname);
if(tagname.equalsIgnoreCase("option"))
{
//System.out.println("in option id="+id+" text="+text);
if(text!=null)
{
//System.out.println("text is not null");
//database.insert(tagname, input_type, id);
database.insert_into_sub_field(id, text, null);
}
}
else
{
//System.out.println("else part id="+id+" text="+text+"tag name ="+tagname+" input_type"+input_type+" desc="+desc);
if(!tagname.equalsIgnoreCase("product"))
{
database.insert(tagname, input_type, id);
database.insert_into_sub_field(id, text,desc);
}
}
}// end of tagname !=null
}
eventType = xmlPullParser.nextToken();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}