1个回答
展开全部
public java.util.List selectxml(String filename)
{
org.w3c.dom.Document dt = null;
javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory
.newInstance();
try
{
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
java.io.File file = new java.io.File(filename);
dt = db.parse(file);
} catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
org.w3c.dom.NodeList nl = dt.getDocumentElement().getElementsByTagName(
"student");
java.util.ArrayList al = new java.util.ArrayList();
System.out.print(nl.getLength());
for (int i = 0; i < nl.getLength(); i++)
{
org.w3c.dom.Element student = (org.w3c.dom.Element) nl.item(i);
Person person = new Person();
person.setName(student.getElementsByTagName("name").item(0)
.getFirstChild().getNodeValue().toLowerCase());
person.setAge(student.getElementsByTagName("age").item(0)
.getFirstChild().getNodeValue().toString());
person.setAddress(student.getElementsByTagName("address").item(0)
.getFirstChild().getNodeValue().toString());
person.setTelephone(student.getElementsByTagName("telephone").item(
0).getFirstChild().getNodeValue().toString());
al.add(person);
}
return al;
}
{
org.w3c.dom.Document dt = null;
javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory
.newInstance();
try
{
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
java.io.File file = new java.io.File(filename);
dt = db.parse(file);
} catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
org.w3c.dom.NodeList nl = dt.getDocumentElement().getElementsByTagName(
"student");
java.util.ArrayList al = new java.util.ArrayList();
System.out.print(nl.getLength());
for (int i = 0; i < nl.getLength(); i++)
{
org.w3c.dom.Element student = (org.w3c.dom.Element) nl.item(i);
Person person = new Person();
person.setName(student.getElementsByTagName("name").item(0)
.getFirstChild().getNodeValue().toLowerCase());
person.setAge(student.getElementsByTagName("age").item(0)
.getFirstChild().getNodeValue().toString());
person.setAddress(student.getElementsByTagName("address").item(0)
.getFirstChild().getNodeValue().toString());
person.setTelephone(student.getElementsByTagName("telephone").item(
0).getFirstChild().getNodeValue().toString());
al.add(person);
}
return al;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询