0

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();
    }
4

1 回答 1

0

如果您不想将空白存储在数据库中,则可以text.trim()在保存到数据库之前进行

于 2014-03-21T07:37:47.740 回答