java代码中读写xml文件、读excel文件

dom4j读写xml文件:参考地址: http://www.cnblogs.com/yjmyzz/archive/2012/11/11/2765312.html
一、需要读excel文件中的数据写入xml文件中
public interface XmlDocument {
	
	/** 
	* 建立XML文档 
	* @param fileName 文件全路径名称 
	*/ 
	public void createXml(String fileName); 
	/** 
	* 解析XML文档 
	* @param fileName 文件全路径名称 
	*/ 
	public void parserXml(String fileName); 
}


二、读excel文件并写入xml文件的test类
@Test
	public void createXml() throws IOException {
		String fileName = "d://test.xml";
		
		Document document = DocumentHelper.createDocument();
		//读取excel 
		String excelFilename = "d://1.xls";
		
		Element employees = document.addElement("keywords");
		Element employee = employees.addElement("fistkeywords");
		Element name = employee.addElement("keywords");
		name.setText("机械及行业设备");
		Element code = employee.addElement("code");
		code.setText("113");
		
		Element thisrdkeywords = employee.addElement("thirdkeywords");
		
		InputStream is;
		is = new FileInputStream(excelFilename);
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
        	HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
            	 HSSFRow hssfRow = hssfSheet.getRow(rowNum);
            	 if (hssfRow == null) {
                     continue;
                 }
                 // 循环列Cell
                 // 0关键词 1code
                 HSSFCell keycell0 = hssfRow.getCell(0);
                 keycell0.setCellType(HSSFCell.CELL_TYPE_STRING);
                 String ssss = keycell0.getStringCellValue();
                 String keyexcel = getValue(keycell0); 
                 
                 //xml文件中插入值
                 Element namethird = thisrdkeywords.addElement("keywords");
         		 namethird.setText(keyexcel);
         		 
         		 HSSFCell codecell2 = hssfRow.getCell(1);
         		codecell2.setCellType(HSSFCell.CELL_TYPE_STRING);
         		 String str=hssfRow.getCell(1).toString();
         		 
         		 Element codethiird = thisrdkeywords.addElement("code");
         		 codethiird.setText(getValue(codecell2));
            }
        }
		
		try {
			OutputFormat format = OutputFormat.createCompactFormat();
			format.setEncoding("utf-8");
			format.setNewlines(true);
			Writer fileWriter = new FileWriter(fileName);
			XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}

	}

三、读xml文件的类
public void parserXml() {
		String fileName = "d://test.xml";
		File inputXml = new File(fileName);
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(inputXml);
			Element employees = document.getRootElement();
			for (Iterator i = employees.elementIterator(); i.hasNext();) {
				Element employee = (Element) i.next();
				for (Iterator j = employee.elementIterator(); j.hasNext();) {
					Element node = (Element) j.next();
					System.out.println(node.getName() + ":" + node.getText());
				}

			}
		} catch (DocumentException e) {
			System.out.println(e.getMessage());
		}
		System.out.println("dom4j parserXml");
	}
	

四、根据xml文件属性查找值
SAXReader reader = new SAXReader();
			Document doc = reader.read(FILENAME); //加载xml文件
			Element key = (Element) doc.selectSingleNode("//keywords[@codeValue='"+code+"']"); //查找“codeValue属性”=113101101的keywords元素
			if(key != null){
				keyword = key.getStringValue();
			}

五、xml文件
<keywordsManager>
<keywords codeValue="113">机械及行业设备</keywords>
<code name="机械及行业设备">113</code>
<keywords codeValue="113101101">加料再生破碎机</keywords>
<code name="加料再生破碎机">113101101</code>
<keywords codeValue="113101102">压塑机</keywords>
<code name="压塑机">113101102</code>
<keywords codeValue="113213101">变速机</keywords>
<code name="变速机">113213101</code>
</keywordsManager>

你可能感兴趣的:(java,xml,Excel)