1.读取XML文件
(1)inputStream类型
(2)转成string类型
2.解析XML文件
(1)String类型输入
(2)inputstream类型
将红色部分换成如下代码
[code]
Document document = null;
try
{
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
document = builder.parse(xmlData);
}
catch (SAXException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
Element element = document.getDocumentElement();[/code]
<?xml version="1.0" encoding="UTF-8"?>
<userlist>
<user>
<name>zhangsan</name>
<mobile>123456789</mobile>
<email>zhangsan@125.com</email>
<birthday>1980-06-26</birthday>
</user>
<user>
<name>lisi</name>
<mobile>156748951</mobile>
<email>lisi@125.com</email>
<birthday>1989-06-06</birthday>
</user>
</userlist>
(1)inputStream类型
InputStream input = this.getClass().getClassLoader().getResourceAsStream("user.xml");
(2)转成string类型
InputStream input = this.getClass().getClassLoader().getResourceAsStream("user.xml");
SAXReader saxReader = new SAXReader();
Document document = null;
String xmlData = document.asXML();
2.解析XML文件
(1)String类型输入
List<User> userList = new ArrayList<User>(10);
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
[b]InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlData));[/color]
//转换成document后解析,不管是inputsteam或者string
Document doc = db.parse(is);[/b]
NodeList usersNodes = doc.getElementsByTagName("user");
for (int i = 0; i < usersNodes.getLength(); i++)
{
Element usersElement = (Element)usersNodes.item(i);
User user = new User();
NodeList childNodes = usersElement.getChildNodes();
// System.out.println("*****"+childNodes.getLength());
for (int j = 0; j < childNodes.getLength(); j++)
{
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE)
{
if ("name".equals(childNodes.item(j).getNodeName()))
{
user.setName(childNodes.item(j).getFirstChild().getNodeValue());
}
else if ("mobile".equals(childNodes.item(j).getNodeName()))
{
user.setMobile(childNodes.item(j).getFirstChild().getNodeValue());
}
else if ("email".equals(childNodes.item(j).getNodeName()))
{
user.setEmail(childNodes.item(j).getFirstChild().getNodeValue());
}
else if ("birthday".equals(childNodes.item(j).getNodeName()))
{
String dataString = childNodes.item(j).getFirstChild().getNodeValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date date = sdf.parse(dataString);//字符串转时间
user.setBirthday(date);
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
}//end for j
userList.add(user);
}
}
catch (Exception e)
{
e.printStackTrace();
}
(2)inputstream类型
将红色部分换成如下代码
[code]
Document document = null;
try
{
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
document = builder.parse(xmlData);
}
catch (SAXException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
Element element = document.getDocumentElement();[/code]