public class Test04Activity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
这里应该如何写代码显示下面解析的xml文件返回的list中的内容?
}
/**
* Dom 解析
*
*
*/
public class DomParse
{
/**
* 输入流
*/
public InputStream is = null;
/**
* 用户数组
*/
public List<English> list = null;
/**
* 构造,给2个属性赋值
*
*/
public DomParse(InputStream is)
{
this.is = is;
list = new ArrayList<English>();
}
/**
* 解析输入流,并返回用户数组
* @return List<English>
*/
public List<English> readXml()
{
//生成工厂,
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
//获取DocumentBuilder
DocumentBuilder db = dbf.newDocumentBuilder();
try
{
//获得Document
Document doc = db.parse(is);
//获取根节点
Element root = doc.getDocumentElement();
//获取节点
NodeList node = root.getElementsByTagName("English");
//获取子节点,并取得text进行赋值
for (int i = 0; i < node.getLength(); i++)
{
English e = new English();
Element English = (Element) node.item(i);
NodeList childnode = English.getChildNodes();
for (int j = 0; j < childnode.getLength(); j++)
{
Node node1 = childnode.item(j);
//判断是否为元素节点
if (node1.getNodeType() == Node.ELEMENT_NODE)
{
Element child = (Element) node1;
if ("id".equals(child.getNodeName()))
{
e.setId(child.getFirstChild().getNodeValue());
}
else if ("text".equals(child.getNodeName()))
{
e.setText(child.getFirstChild().getNodeValue());
}
}
}
list.add(e);
}
is.close();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
//返回对象
return list;
}
}}