【发布时间】:2025-12-25 12:25:06
【问题描述】:
我有一个解析 XML 的函数,当事件类型为文本时,我在其中得到空格。下面解释了代码,其中当事件类型为结束标记时,将空格添加到数据库中。
我的代码如下:
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();
}
【问题讨论】:
标签: android xml-parsing whitespace xmlpullparser android-xmlpullparser