Android 要打印组名及其子数据吗
这里 我试图动态打印特定的组名及其子数据,如标题及其内容 但我能够在标题中弹出组名,这并没有问题 但问题是,我也得到了组名而不是子内容 代码如下: SectionDemo.javaAndroid 要打印组名及其子数据吗,android,parsing,listview,Android,Parsing,Listview,这里 我试图动态打印特定的组名及其子数据,如标题及其内容 但我能够在标题中弹出组名,这并没有问题 但问题是,我也得到了组名而不是子内容 代码如下: SectionDemo.java package com.bestdambikers; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.View; import android
package com.bestdambikers;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SectionedDemo extends ListActivity {
String strUrl = "http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&plg_name=jomsocial&pview=user&ptask=field_list&userid=80&sessionid="+ConstantData.session_id+"&tmpl=component";
DetailBean dBean;
XmlParser parser;
ArrayList<Object> result;
List<DetailBean> list;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.header_main);
//dBean = new DetailBean();
parser = new XmlParser(strUrl, new DetailBean());
result = parser.ParseUrl("data", "group");
int r = result.size();
for(int i=0; i<result.size(); i++)
{
dBean = (DetailBean)result.get(i);
list=Arrays.asList(dBean);
Collections.shuffle(list);
adapter.addSection(dBean.group_name,
new ArrayAdapter<DetailBean>(this,
android.R.layout.simple_list_item_1,
list));
}
setListAdapter(adapter);
}
SectionedAdapter adapter=new SectionedAdapter() {
protected View getHeaderView(String caption, int index,
View convertView,
ViewGroup parent) {
TextView result=(TextView)convertView;
if (convertView==null) {
result=(TextView)getLayoutInflater()
.inflate(R.layout.header,
null);
}
result.setText(caption);
return(result);
}
};
}
我要解析的xml文件
<data>
<code>1</code>
<fields>
<group>
<group_name>Basic Information</group_name>
<field>
<id>2</id>
<name>Gender</name>
<value>female</value>
<required>1</required>
<type>select</type>
</field>
<field>
<id>3</id>
<name>Birthday</name>
<value>05-06-2011</value>
<required>1</required>
<type>date</type>
</field>
<field>
<id>4</id>
<name>About me</name>
<value>Well meet me u will come to know</value>
<required>1</required>
<type>textarea</type>
</field>
</group>
<group>
<group_name>Contact Information</group_name>
<field>
<id>6</id>
<name>Mobile phone</name>
<value>5555555555</value>
<required>0</required>
<type>text</type>
</field>
<field>
<id>7</id>
<name>Land phone</name>
<value>6666666666</value>
<required>0</required>
<type>text</type>
</field>
<field>
<id>8</id>
<name>Address</name>
<value>Tassel global</value>
<required>1</required>
<type>textarea</type>
</field>
<field>
<id>9</id>
<name>State</name>
<value>Gujarat</value>
<required>1</required>
<type>text</type>
</field>
<field>
<id>10</id>
<name>City / Town</name>
<value>Ahmedabad</value>
<required>1</required>
<type>text</type>
</field>
<field>
<id>11</id>
<name>Country</name>
<value>India</value>
<required>1</required>
<type>country</type>
</field>
<field>
<id>12</id>
<name>Website</name>
<value>http://www.google.com</value>
<required>1</required>
<type>url</type>
</field>
</group>
<group>
<group_name>Education</group_name>
<field>
<id>14</id>
<name>College / University</name>
<value>California university</value>
<required>1</required>
<type>text</type>
</field>
<field>
<id>15</id>
<name>Graduation Year</name>
<value>2010</value>
<required>1</required>
<type>text</type>
</field>
</group>
</fields>
</data>
XmlParser.java
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XmlParser extends DefaultHandler
{
public String RootElement;
public String RecordElement;
public InputStream in;
public Object mainObj;
public Object newObj;
public boolean inProcess;
public String xmlURL;
public ArrayList<Object> Records = null;
private final String TAG = "XmlParser";
StringBuffer buffer = new StringBuffer();
String elementName;
String elementValue;
public XmlParser(final InputStream is, final Object tempObj)
{
this.in = is;
this.mainObj = tempObj;
Log.i("Object value", "" + this.mainObj);
this.inProcess = false;
}
public XmlParser(final String strURL, final Object tempObj)
{
this.xmlURL = strURL;
this.mainObj = tempObj;
this.inProcess = false;
}
public ArrayList<Object> ParseUrl(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
Log.i("RootElement", this.RootElement);
this.RecordElement = recordElement;
Log.i("RecordElement", this.RecordElement);
try
{
final URL sourceUrl = new URL(this.xmlURL);
Log.d("URl", this.xmlURL);
final SAXParserFactory spf = SAXParserFactory.newInstance();
final SAXParser sp = spf.newSAXParser();
final XMLReader reader = sp.getXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource(sourceUrl.openStream()));
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
public ArrayList<Object> parse(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
this.RecordElement = recordElement;
Log.i("Root Element", "" + this.RootElement);
Log.i("Record Element", "" + this.RecordElement);
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(this.in, this);
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
@Override
public void startElement(final String Uri, final String localName,
final String qName, final Attributes attributes)throws SAXException
{
Log.d("URl", this.xmlURL);
this.elementValue = "";
Log.i("IN STARTELEMENT", "" + this.elementValue);
if (localName.length() > 0)
{
Log.i("Local Name Length", "" + localName.length());
Log.i("LocalName", "" + localName);
if (localName.equalsIgnoreCase(this.RootElement))
{
this.Records = new ArrayList<Object>();
Log.i("Root element", "" + this.RootElement);
Log.i("Records", "" + this.Records);
}
else
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.newObj = ClassUtils.newObject(this.mainObj);
Log.i("Main Object", "" + this.mainObj);
Log.i("Record element", "" + this.RecordElement);
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
Log.i("Element Value", "" + this.elementValue);
this.inProcess = true;
}
}
}
@Override
public void characters(final char[] ch, final int start,
final int length) throws SAXException
{
this.elementValue += new String(ch, start, length).trim();
Log.i("CHARACTERS VALUE", "" + this.elementValue);
}
@Override
public void endElement(final String Uri, final String localName,
final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.Records.add(this.newObj);
this.inProcess = false;
}
else
if (this.inProcess)
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
}
}
import java.util.ArrayList;
public class DetailBean
{
private String groupName = null;
private int code;
private ArrayList<Field> fields;
public String getGroupName()
{
return groupName;
}
public void setGroupName(String groupName)
{
this.groupName = groupName;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public ArrayList<Field> getFields()
{
return fields;
}
public void setFields(ArrayList<Field> fields)
{
this.fields = fields;
}
@Override
public String toString()
{
// You should populate this string with the data
// you need inside the TextView
return this.groupName;
}
}
public class Field
{
private int id;
private String name;
private String value;
private boolean required;
private String type;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public boolean isRequired()
{
return required;
}
public void setRequired(boolean required)
{
this.required = required;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
}
public class XmlHandler extends DefaultHandler
{
private static final String TAG_GROUP = "group";
private static final String TAG_GROUPNAME = "group_name";
private static final String TAG_FIELD = "field";
private static final String TAG_CODE = "code";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_VALUE = "value";
private static final String TAG_REQUIRED = "required";
private static final String TAG_TYPE = "type";
private int code = 0;
private String currentNodeName;
private DetailBean currentGroup;
private Field currentField;
private ArrayList<DetailBean> records = null;
private String elementValue;
public ArrayList<DetailBean> getRecords()
{
return records;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
this.records = new ArrayList<DetailBean>();
}
@Override
public void startElement(final String Uri, final String localName, final String qName,
final Attributes attributes) throws SAXException
{
if (localName != null)
{
currentNodeName = localName;
}
}
@Override
public void characters(final char[] ch, final int start, final int length) throws SAXException
{
if (this.currentNodeName == null)
return;
this.elementValue = new String(ch, start, length).trim();
if (this.currentNodeName.equalsIgnoreCase(TAG_CODE))
{
this.code = Integer.parseInt(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUP))
{
this.currentGroup = new DetailBean();
this.currentGroup.setCode(this.code);
this.currentGroup.setFields(new ArrayList<Field>());
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUPNAME))
{
this.currentGroup.setGroupName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_FIELD))
{
this.currentField = new Field();
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_ID))
{
this.currentField.setId(Integer.parseInt(this.elementValue));
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_NAME))
{
this.currentField.setName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_VALUE))
{
this.currentField.setValue(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_REQUIRED))
{
this.currentField.setRequired(Integer.parseInt(this.elementValue) > 0);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
{
this.currentField.setType(this.elementValue);
}
}
@Override
public void endElement(final String Uri, final String localName, final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(TAG_GROUP))
{
if (this.currentGroup != null)
this.records.add(this.currentGroup);
}
else if (localName.equalsIgnoreCase(TAG_FIELD))
{
if ((this.currentGroup != null) && (this.currentField != null))
this.currentGroup.getFields().add(this.currentField);
}
currentNodeName = null;
}
}
public class XmlParser
{
public ArrayList<DetailBean> parseFromUrl(final String xmlURL)
{
try
{
final URL sourceUrl = new URL(xmlURL);
Log.d("URL", xmlURL);
final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
final XMLReader reader = sp.getXMLReader();
final XmlHandler handler = new XmlHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(sourceUrl.openStream()));
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from url", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputStream(final InputStream in)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(in, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputStream", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputSource(final InputSource is)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(is, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputSource", e);
return null;
}
}
}
import java.io.InputStream;
导入java.net.URL;
导入java.util.ArrayList;
导入javax.xml.parsers.SAXParser;
导入javax.xml.parsers.SAXParserFactory;
导入org.xml.sax.Attributes;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
导入org.xml.sax.XMLReader;
导入org.xml.sax.helpers.DefaultHandler;
导入android.util.Log;
公共类XmlParser扩展了DefaultHandler
{
公共字符串根元素;
公共字符串记录元素;
公共输入流;
公共对象管理;
公共对象newObj;
公共布尔过程;
公共字符串xmlURL;
公共ArrayList记录=null;
私有最终字符串TAG=“XmlParser”;
StringBuffer=新的StringBuffer();
字符串元素名;
字符串元素值;
公共XmlParser(最终输入流为,最终对象tempObj)
{
this.in=is;
this.mainObj=tempObj;
Log.i(“对象值”,“this.mainObj”);
this.inProcess=false;
}
公共XmlParser(最终字符串strURL,最终对象tempObj)
{
this.xmlURL=strURL;
this.mainObj=tempObj;
this.inProcess=false;
}
公共ArrayList ParseUrl(最终字符串根元素,最终字符串记录元素)
{
this.RootElement=RootElement;
Log.i(“RootElement”,this.RootElement);
this.RecordElement=RecordElement;
Log.i(“RecordElement”,this.RecordElement);
尝试
{
最终URL sourceUrl=新URL(this.xmlURL);
Log.d(“URl”,this.xmlURL);
final SAXParserFactory spf=SAXParserFactory.newInstance();
final SAXParser sp=spf.newSAXParser();
最终XMLReader=sp.getXMLReader();
setContentHandler(this);
parse(新的InputSource(sourceUrl.openStream());
}
捕获(最终异常e)
{
e、 printStackTrace();
返回null;
}
归还此记录;
}
公共ArrayList解析(最终字符串根元素、最终字符串记录元素)
{
this.RootElement=RootElement;
this.RecordElement=RecordElement;
Log.i(“根元素”,“”+this.RootElement);
Log.i(“记录元素”,即“+this.RecordElement”);
尝试
{
最终SAXParser=SAXParserFactory.newInstance().newSAXParser();
parser.parse(this.in,this);
}
捕获(最终异常e)
{
e、 printStackTrace();
返回null;
}
归还此记录;
}
@凌驾
public void startElement(最终字符串Uri、最终字符串localName、,
最终字符串(qName、最终属性)引发SAXException
{
Log.d(“URl”,this.xmlURL);
this.elementValue=“”;
Log.i(“在STARTELEMENT中,”“+this.elementValue);
if(localName.length()>0)
{
Log.i(“本地名称长度”,“localName.Length());
Log.i(“LocalName”和“+LocalName”);
if(localName.equalsIgnoreCase(this.RootElement))
{
this.Records=new ArrayList();
Log.i(“根元素”,“”+this.RootElement);
Log.i(“记录”、“+this.Records”);
}
其他的
if(localName.equalsIgnoreCase(this.RecordElement))
{
this.newObj=ClassUtils.newObject(this.mainObj);
Log.i(“主对象”,“this.mainObj”);
Log.i(“记录元素”,即“+this.RecordElement”);
objectMapping(this.newObj、localName、this.elementValue);
Log.i(“元素值”,“”+this.elementValue);
this.inProcess=true;
}
}
}
@凌驾
公共无效字符(最终字符[]ch,最终整数开始,
最终整数长度)引发异常
{
this.elementValue+=新字符串(ch,start,length).trim();
Log.i(“CHARACTERS VALUE”,“this.elementValue”);
}
@凌驾
public void endElement(最终字符串Uri、最终字符串localName、,
最后一个字符串(qName)引发SAXException
{
if(localName.equalsIgnoreCase(this.RecordElement))
{
this.Records.add(this.newObj);
this.inProcess=false;
}
其他的
if(this.inProcess)
objectMapping(this.newObj、localName、this.elementValue);
}
}
您的xml表明您正在处理分层数据结构,因此我的建议是使用带有适当的适配器的ExpandableListView
您的bean结构也应该重新考虑,以便能够保持层次结构。为此,您应该拆分DetailBean
类,只保留groupName
、code
和字段
成员:
DetailBean.java
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XmlParser extends DefaultHandler
{
public String RootElement;
public String RecordElement;
public InputStream in;
public Object mainObj;
public Object newObj;
public boolean inProcess;
public String xmlURL;
public ArrayList<Object> Records = null;
private final String TAG = "XmlParser";
StringBuffer buffer = new StringBuffer();
String elementName;
String elementValue;
public XmlParser(final InputStream is, final Object tempObj)
{
this.in = is;
this.mainObj = tempObj;
Log.i("Object value", "" + this.mainObj);
this.inProcess = false;
}
public XmlParser(final String strURL, final Object tempObj)
{
this.xmlURL = strURL;
this.mainObj = tempObj;
this.inProcess = false;
}
public ArrayList<Object> ParseUrl(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
Log.i("RootElement", this.RootElement);
this.RecordElement = recordElement;
Log.i("RecordElement", this.RecordElement);
try
{
final URL sourceUrl = new URL(this.xmlURL);
Log.d("URl", this.xmlURL);
final SAXParserFactory spf = SAXParserFactory.newInstance();
final SAXParser sp = spf.newSAXParser();
final XMLReader reader = sp.getXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource(sourceUrl.openStream()));
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
public ArrayList<Object> parse(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
this.RecordElement = recordElement;
Log.i("Root Element", "" + this.RootElement);
Log.i("Record Element", "" + this.RecordElement);
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(this.in, this);
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
@Override
public void startElement(final String Uri, final String localName,
final String qName, final Attributes attributes)throws SAXException
{
Log.d("URl", this.xmlURL);
this.elementValue = "";
Log.i("IN STARTELEMENT", "" + this.elementValue);
if (localName.length() > 0)
{
Log.i("Local Name Length", "" + localName.length());
Log.i("LocalName", "" + localName);
if (localName.equalsIgnoreCase(this.RootElement))
{
this.Records = new ArrayList<Object>();
Log.i("Root element", "" + this.RootElement);
Log.i("Records", "" + this.Records);
}
else
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.newObj = ClassUtils.newObject(this.mainObj);
Log.i("Main Object", "" + this.mainObj);
Log.i("Record element", "" + this.RecordElement);
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
Log.i("Element Value", "" + this.elementValue);
this.inProcess = true;
}
}
}
@Override
public void characters(final char[] ch, final int start,
final int length) throws SAXException
{
this.elementValue += new String(ch, start, length).trim();
Log.i("CHARACTERS VALUE", "" + this.elementValue);
}
@Override
public void endElement(final String Uri, final String localName,
final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.Records.add(this.newObj);
this.inProcess = false;
}
else
if (this.inProcess)
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
}
}
import java.util.ArrayList;
public class DetailBean
{
private String groupName = null;
private int code;
private ArrayList<Field> fields;
public String getGroupName()
{
return groupName;
}
public void setGroupName(String groupName)
{
this.groupName = groupName;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public ArrayList<Field> getFields()
{
return fields;
}
public void setFields(ArrayList<Field> fields)
{
this.fields = fields;
}
@Override
public String toString()
{
// You should populate this string with the data
// you need inside the TextView
return this.groupName;
}
}
public class Field
{
private int id;
private String name;
private String value;
private boolean required;
private String type;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public boolean isRequired()
{
return required;
}
public void setRequired(boolean required)
{
this.required = required;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
}
public class XmlHandler extends DefaultHandler
{
private static final String TAG_GROUP = "group";
private static final String TAG_GROUPNAME = "group_name";
private static final String TAG_FIELD = "field";
private static final String TAG_CODE = "code";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_VALUE = "value";
private static final String TAG_REQUIRED = "required";
private static final String TAG_TYPE = "type";
private int code = 0;
private String currentNodeName;
private DetailBean currentGroup;
private Field currentField;
private ArrayList<DetailBean> records = null;
private String elementValue;
public ArrayList<DetailBean> getRecords()
{
return records;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
this.records = new ArrayList<DetailBean>();
}
@Override
public void startElement(final String Uri, final String localName, final String qName,
final Attributes attributes) throws SAXException
{
if (localName != null)
{
currentNodeName = localName;
}
}
@Override
public void characters(final char[] ch, final int start, final int length) throws SAXException
{
if (this.currentNodeName == null)
return;
this.elementValue = new String(ch, start, length).trim();
if (this.currentNodeName.equalsIgnoreCase(TAG_CODE))
{
this.code = Integer.parseInt(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUP))
{
this.currentGroup = new DetailBean();
this.currentGroup.setCode(this.code);
this.currentGroup.setFields(new ArrayList<Field>());
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUPNAME))
{
this.currentGroup.setGroupName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_FIELD))
{
this.currentField = new Field();
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_ID))
{
this.currentField.setId(Integer.parseInt(this.elementValue));
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_NAME))
{
this.currentField.setName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_VALUE))
{
this.currentField.setValue(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_REQUIRED))
{
this.currentField.setRequired(Integer.parseInt(this.elementValue) > 0);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
{
this.currentField.setType(this.elementValue);
}
}
@Override
public void endElement(final String Uri, final String localName, final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(TAG_GROUP))
{
if (this.currentGroup != null)
this.records.add(this.currentGroup);
}
else if (localName.equalsIgnoreCase(TAG_FIELD))
{
if ((this.currentGroup != null) && (this.currentField != null))
this.currentGroup.getFields().add(this.currentField);
}
currentNodeName = null;
}
}
public class XmlParser
{
public ArrayList<DetailBean> parseFromUrl(final String xmlURL)
{
try
{
final URL sourceUrl = new URL(xmlURL);
Log.d("URL", xmlURL);
final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
final XMLReader reader = sp.getXMLReader();
final XmlHandler handler = new XmlHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(sourceUrl.openStream()));
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from url", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputStream(final InputStream in)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(in, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputStream", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputSource(final InputSource is)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(is, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputSource", e);
return null;
}
}
}
我还拆分了XmlParser类,使处理程序部分位于另一个类中:
XmlHandler.java
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XmlParser extends DefaultHandler
{
public String RootElement;
public String RecordElement;
public InputStream in;
public Object mainObj;
public Object newObj;
public boolean inProcess;
public String xmlURL;
public ArrayList<Object> Records = null;
private final String TAG = "XmlParser";
StringBuffer buffer = new StringBuffer();
String elementName;
String elementValue;
public XmlParser(final InputStream is, final Object tempObj)
{
this.in = is;
this.mainObj = tempObj;
Log.i("Object value", "" + this.mainObj);
this.inProcess = false;
}
public XmlParser(final String strURL, final Object tempObj)
{
this.xmlURL = strURL;
this.mainObj = tempObj;
this.inProcess = false;
}
public ArrayList<Object> ParseUrl(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
Log.i("RootElement", this.RootElement);
this.RecordElement = recordElement;
Log.i("RecordElement", this.RecordElement);
try
{
final URL sourceUrl = new URL(this.xmlURL);
Log.d("URl", this.xmlURL);
final SAXParserFactory spf = SAXParserFactory.newInstance();
final SAXParser sp = spf.newSAXParser();
final XMLReader reader = sp.getXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource(sourceUrl.openStream()));
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
public ArrayList<Object> parse(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
this.RecordElement = recordElement;
Log.i("Root Element", "" + this.RootElement);
Log.i("Record Element", "" + this.RecordElement);
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(this.in, this);
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
@Override
public void startElement(final String Uri, final String localName,
final String qName, final Attributes attributes)throws SAXException
{
Log.d("URl", this.xmlURL);
this.elementValue = "";
Log.i("IN STARTELEMENT", "" + this.elementValue);
if (localName.length() > 0)
{
Log.i("Local Name Length", "" + localName.length());
Log.i("LocalName", "" + localName);
if (localName.equalsIgnoreCase(this.RootElement))
{
this.Records = new ArrayList<Object>();
Log.i("Root element", "" + this.RootElement);
Log.i("Records", "" + this.Records);
}
else
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.newObj = ClassUtils.newObject(this.mainObj);
Log.i("Main Object", "" + this.mainObj);
Log.i("Record element", "" + this.RecordElement);
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
Log.i("Element Value", "" + this.elementValue);
this.inProcess = true;
}
}
}
@Override
public void characters(final char[] ch, final int start,
final int length) throws SAXException
{
this.elementValue += new String(ch, start, length).trim();
Log.i("CHARACTERS VALUE", "" + this.elementValue);
}
@Override
public void endElement(final String Uri, final String localName,
final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.Records.add(this.newObj);
this.inProcess = false;
}
else
if (this.inProcess)
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
}
}
import java.util.ArrayList;
public class DetailBean
{
private String groupName = null;
private int code;
private ArrayList<Field> fields;
public String getGroupName()
{
return groupName;
}
public void setGroupName(String groupName)
{
this.groupName = groupName;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public ArrayList<Field> getFields()
{
return fields;
}
public void setFields(ArrayList<Field> fields)
{
this.fields = fields;
}
@Override
public String toString()
{
// You should populate this string with the data
// you need inside the TextView
return this.groupName;
}
}
public class Field
{
private int id;
private String name;
private String value;
private boolean required;
private String type;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public boolean isRequired()
{
return required;
}
public void setRequired(boolean required)
{
this.required = required;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
}
public class XmlHandler extends DefaultHandler
{
private static final String TAG_GROUP = "group";
private static final String TAG_GROUPNAME = "group_name";
private static final String TAG_FIELD = "field";
private static final String TAG_CODE = "code";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_VALUE = "value";
private static final String TAG_REQUIRED = "required";
private static final String TAG_TYPE = "type";
private int code = 0;
private String currentNodeName;
private DetailBean currentGroup;
private Field currentField;
private ArrayList<DetailBean> records = null;
private String elementValue;
public ArrayList<DetailBean> getRecords()
{
return records;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
this.records = new ArrayList<DetailBean>();
}
@Override
public void startElement(final String Uri, final String localName, final String qName,
final Attributes attributes) throws SAXException
{
if (localName != null)
{
currentNodeName = localName;
}
}
@Override
public void characters(final char[] ch, final int start, final int length) throws SAXException
{
if (this.currentNodeName == null)
return;
this.elementValue = new String(ch, start, length).trim();
if (this.currentNodeName.equalsIgnoreCase(TAG_CODE))
{
this.code = Integer.parseInt(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUP))
{
this.currentGroup = new DetailBean();
this.currentGroup.setCode(this.code);
this.currentGroup.setFields(new ArrayList<Field>());
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUPNAME))
{
this.currentGroup.setGroupName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_FIELD))
{
this.currentField = new Field();
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_ID))
{
this.currentField.setId(Integer.parseInt(this.elementValue));
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_NAME))
{
this.currentField.setName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_VALUE))
{
this.currentField.setValue(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_REQUIRED))
{
this.currentField.setRequired(Integer.parseInt(this.elementValue) > 0);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
{
this.currentField.setType(this.elementValue);
}
}
@Override
public void endElement(final String Uri, final String localName, final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(TAG_GROUP))
{
if (this.currentGroup != null)
this.records.add(this.currentGroup);
}
else if (localName.equalsIgnoreCase(TAG_FIELD))
{
if ((this.currentGroup != null) && (this.currentField != null))
this.currentGroup.getFields().add(this.currentField);
}
currentNodeName = null;
}
}
public class XmlParser
{
public ArrayList<DetailBean> parseFromUrl(final String xmlURL)
{
try
{
final URL sourceUrl = new URL(xmlURL);
Log.d("URL", xmlURL);
final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
final XMLReader reader = sp.getXMLReader();
final XmlHandler handler = new XmlHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(sourceUrl.openStream()));
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from url", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputStream(final InputStream in)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(in, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputStream", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputSource(final InputSource is)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(is, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputSource", e);
return null;
}
}
}
您的SectionedDemo
活动应该扩展ExpandableListActivity
而不是ListActivity
,其onCreate
方法应该如下所示:
@凌驾
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
getExpandableListView().setGroupIndicator(null);
getExpandableListView().setDivider(null);
getExpandableListView().setDividerHeight(0);
parser = new XmlParser();
result = parser.parseFromUrl(XML_URL);
adapter = new MyExpandableListAdapter();
setListAdapter(adapter);
}
下面是一个应用程序的示例输出,按照上面的说明,使用您提供的xml源:
下面是我的主要活动类,用于生成此输出(使用