dom4j读取xml

1,导入DOM4J的jar包

2,代码、:

 

这里我用到的是把读取出来的xml里面的某些节点导入到数据库中

 

public ArrayList <CfsdcResourceOtherInfoBean> getXML(String filename) throws DocumentException{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(filename));
		Element root = document.getRootElement();//<Nodes></Nodes>
		ArrayList <CfsdcResourceOtherInfoBean> webgises = new ArrayList<CfsdcResourceOtherInfoBean>();
		//遍历根节点内的每个节点,即每个<NodeInfo>
		for (Iterator it = root.elementIterator();it.hasNext();){
			Element element = (Element) it.next();
			//每一个Node用list里面一个元素存储
			CfsdcResourceOtherInfoBean onegis = new CfsdcResourceOtherInfoBean();
			onegis.setOtheract("0");
			for ( Iterator iterInner = element.elementIterator(); iterInner.hasNext(); ) {
				Element elementInner = (Element) iterInner.next();
				if (elementInner.getName().toLowerCase().equals("id")){
					onegis.setResourceId(Integer.parseInt(elementInner.getText()));
				}else if (elementInner.getName().toLowerCase().equals("ddcFileName".toLowerCase())){
					onegis.setDdcFileName(elementInner.getText());
				}else if (elementInner.getName().toLowerCase().equals("csfFileName".toLowerCase())){
					onegis.setCsfFileName(elementInner.getText());
				}else if (elementInner.getName().toLowerCase().equals("fileName".toLowerCase())){
					onegis.setFileName(elementInner.getText());
				}else if (elementInner.getName().toLowerCase().equals("description".toLowerCase())){
					onegis.setDescription(elementInner.getText());
				}else if (elementInner.getName().toLowerCase().equals("UrlLocation".toLowerCase())){
					onegis.setOtherurl(elementInner.getText());
				}
			//	hm.put(elementInner.getName(), elementInner.getText());
			}
			//使用时if判断取消注释
			if (!(onegis.getOtherurl()==null)&&!onegis.getOtherurl().trim().equals(""))
				webgises.add(onegis);
		}
		return webgises;
	}

 

你可能感兴趣的:(dom4j)