<?xml version="1.0" encoding="UTF-8"?>
<data>
<row>
<id>1</id>
<name>JackieChen</name>
</row>
<row>
<id>2</id>
<name>TomLee</name>
</row>
</data>
package com.admans.mvc.dao;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class CustomerDaoXMLImpl implements CustomerDao {
@Override
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc) {
// TODO Auto-generated method stub
}
@Override
public List<Customer> getAll() {
SAXReader reader = new SAXReader();
Document document = null;
List<Customer> lstCustomers = new ArrayList<Customer>();
//coustomers.xml 文件存在于 WEB-INF/classes (src)目录
String fileName = this.getClass().getResource("/").getPath() + "Customers.xml";
try {
document = reader.read(new File(fileName));
} catch (DocumentException e) {
e.printStackTrace();
}
String xpath = "/data/row";
List<Node> list = document.selectNodes(xpath);
for (Node node : list) {
Element eId = (Element) node.selectObject("id");
Integer idInteger = Integer.parseInt(eId.getText());
Element eName = (Element) node.selectObject("name");
String name = eName.getText();
lstCustomers.add(new Customer(idInteger, name));
}
return lstCustomers;
}
@Override
public void save(Customer customer) {
// TODO Auto-generated method stub
}
@Override
public Customer get(Integer id) {
// TODO Auto-generated method stub
return null;
}
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
}
@Override
public long getCountWithName(String name) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void update(Customer customer) {
// TODO Auto-generated method stub
}
}
dom4j-2.1.1 及jaxen-1.1.6 下载地址:
https://download.csdn.net/download/admans/11953343
jaxen-1.2.0.jar 下载地址:
https://download.csdn.net/download/admans/11954126
https://repo1.maven.org/maven2/jaxen/jaxen/1.2.0/jaxen-1.2.0.jar