`

jdom读写XMl文件

    博客分类:
  • JAVA
 
阅读更多

昨天写的一个使用jdom读写xml文件的一个例子:

 

/** 生成返回结果XMl*/
	public boolean writeXML(String guid, String path) {
		boolean temp = false;
		List<SynchronousJournal> listSuccess = synchronousJournalManager
				.quereySynchronousJournal(guid);
		List<SynchronousJournal> listError = synchronousJournalManager
				.quereySynchronousJournalErroe(guid);

		File file = new File(path);// 创建一个xml文件

		try {

			Element root = new Element("RESULTLIST");// 创建根元素
			for (SynchronousJournal synchronousJournal : listSuccess) {

				Element result = new Element("RESULT");// 创建子元素
				root.addContent(result);// 添加子元素到根节点

				Element type = new Element("TYPE");// 创建叶子节点
				result.addContent(type);// 添加叶子节点到父节点
				
                Element businessType = new Element("BUSINESSTYPE");// 创建叶子节点
                businessType.addContent(businessType);// 添加叶子节点到父节点
				type.setText(synchronousJournal.getBusinesstype());// 给叶子节点赋值
				/** 成功信息 */
				Element success = new Element("SUCCESS");// 创建叶子节点
				success.setText(synchronousJournal.getCount().toString());// 给叶子节点赋值
				type.addContent(success);// 添加叶子节点到父节点
				int a = 0;
				for (SynchronousJournal synchronousJournal2 : listError) {
					if (synchronousJournal.getBusinesstype().equals(
							synchronousJournal2.getBusinesstype()))
						a++;
				}
				Element error = new Element("ERROR");
				type.addContent(error);

				Element errorcount = new Element("ERRORCOUNT");
				error.setText(String.valueOf(a));// 给叶子节点赋值
				error.addContent(errorcount);

				for (SynchronousJournal synchronousJournal2 : listError) {
					/** 失败信息 */
					if (synchronousJournal.getBusinesstype().equals(
							synchronousJournal2.getBusinesstype())) {
						Element errorguid = new Element("ERRORGUID");
						error.setText(synchronousJournal2.getInfoGuid());// 给叶子节点赋值
						error.addContent(errorguid);
					}
				}
			}
			Document doc = new Document();// 创建文本对象
			doc.addContent(root);// 添加树倒文本中
			Format format = Format.getCompactFormat();
			format.setIndent("     ");
			XMLOutputter out = new XMLOutputter(format);// 创建输出流
			FileWriter fw = new FileWriter(file);// 写数据
			out.output(doc, fw);// 输出到xml文件中
			fw.close();// 关闭写入流
			temp = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		return temp;
	}

	/** 读取返回结果xml*/
	public static boolean readXml(String path) {
		boolean temp = false;
		SAXBuilder sb = new SAXBuilder();// 构建一个JDOM文档输入流
		try {
			Document doc = sb.build(path);// 加载xml
			Element root = doc.getRootElement();// 获得xml文件的内容
			List list = root.getChildren();// 把xml文件内容转化成数组形式
			for (int i = 0; i < list.size(); i++) {
				Element result = (Element) list.get(i);// 获得数组中的一个对象
				List typelist = result.getChildren("TYPE");
				for (int j = 0; j < typelist.size(); j++) {
					Element type = (Element) typelist.get(j);// 获得xml文件的内容
					System.out.println(type.getChildText("BUSINESSTYPE"));
					String success=type.getChildText("SUCCESS");
					System.out.println("成功 "+success+"条数据");

					Element error = type.getChild("ERROR");// 获得xml文件的内容
					String errorCount = error.getChildText("ERRORCOUNT");// 获得对象的其中一个元素
					System.out.println("失败 "+errorCount+"条数据");
					List errorguid = error.getChildren("ERRORGUID");// 把xml文件内容转化成数组形式
					
					String empno1 = "失败数据guid:";
					for (int a = 0; a < errorguid.size(); a++) {
						Element emp1 = (Element) errorguid.get(a);// 获得数组中的一个对象
						empno1 = empno1 + "  " + emp1.getText();
					}
					System.out.println(empno1);
				}
				// }
			}
			temp = true;
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			temp = false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			temp = false;
		}
		return temp;
	}<?xml version="1.0" encoding="UTF-8"?>
<RESULTLIST>
	<RESULT>
		<TYPE>
		<BUSINESSTYPE>定级备案信息</BUSINESSTYPE>
		<SUCCESS>50</SUCCESS>
		<ERROR>
			<ERRORCOUNT>20</ERRORCOUNT>
			<ERRORGUID>1234156456462</ERRORGUID>
			<ERRORGUID>1234156456464</ERRORGUID>
		</ERROR>
		</TYPE>

		<TYPE>
		<BUSINESSTYPE>测评整改</BUSINESSTYPE>
		<SUCCESS>50</SUCCESS>
		<ERROR>
			<ERRORCOUNT>20</ERRORCOUNT>
			<ERRORGUID>1234156456463</ERRORGUID>
			<ERRORGUID>1234156456464</ERRORGUID>
		</ERROR>
		</TYPE>
	</RESULT>

	
</RESULTLIST>
 
分享到:
评论
1 楼 程俊2009 2014-12-22  
需要用哪些jar,可以说下吗

相关推荐

Global site tag (gtag.js) - Google Analytics