项目中使用XML方式去取数据插入数据代码

来源:互联网 发布:淘宝上哪个符点是真的 编辑:程序博客网 时间:2024/05/19 09:02

1、XML部分代码

<Citys><City cityName="南昌市" provinceId="1"></City><City cityName="赣州市" provinceId="1"></City><City cityName="合肥市" provinceId="2"></City><City cityName="长沙市" provinceId="3"></City></Citys>


2、读取数据插入数据中的java代码,使用dom4j
public void addObject(String xmlFilePath) {try {String filePath = null;if (xmlFilePath == null || xmlFilePath.trim().equals("")) {filePath = file;} else {filePath = xmlFilePath;}Document document = new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath));importCity(document.selectNodes("//Citys/City"));} catch (Exception e) {e.printStackTrace();}}

3、解析节点插入数据库中

public void importCity(List<Element> citys) {for (Iterator<Element> iterator = citys.iterator(); iterator.hasNext();) {Element element = (Element) iterator.next();City city = new City();city.setCityName(element.attributeValue("cityName"));city.setpProvinceId(proDao.getProvinceById(Integer.parseInt(element.attributeValue("provinceId"))));super.addObject(city);}}

4、总结,这是部分代码,只能够说明使用方式,不能直接运行的代码。

原创粉丝点击