1. 程式人生 > >Dom4j解析xml && json格式轉換成String

Dom4j解析xml && json格式轉換成String

一,專案中,需要解析xml,使用了dom4j解析:

程式碼中實現是取指定的列的值

public void parserXml(String fileName,String type) {
		File inputXml = new File(fileName);
		SAXReader saxReader = new SAXReader();
		List<String> list = new ArrayList<String>();
		try {
			Document document = saxReader.read(inputXml);
			Element employees = document.getRootElement();
			for (Iterator<Element> i = employees.elementIterator(); i.hasNext();) {
				Element employee = (Element) i.next();
				for (Iterator<Element> j = employee.elementIterator(); j.hasNext();) {
					Element node = (Element) j.next();
					if(node.getName()==type){
						list.add(node.getText());
					}
					
				}
			}
			//去重複資料
			Set<String> set = new HashSet<String>(list);
			System.out.println(list.size());
			System.out.println("***************************");
			for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
				Object object = (Object) iterator.next();
				System.out.println(object);
			}
		} catch (DocumentException e) {
			System.out.println(e.getMessage());
		}
		System.out.println("dom4j parserXml");
	}
	public static void main(String[] args) {
		DO4JParmXML dO4JParmXML = new DO4JParmXML();
		//字符集為gb2312
		dO4JParmXML.parserXml("c:\\Users\\zhanglong.zhanglong-PC\\Desktop\\aaa.xml","num_iid");
		
	}
xml格式:

二,傳入json字串和返回的指定字串

public static void main(String[] args) {
		
		String name = "[{'items_get_response':{'items':{'item':[{'cid':50000697,'delist_time':'2011-12-27 15:40:23','nick':'wangxiangdong0705','num_iid':13315315630,'pic_url':'http://img04.taobaocdn.com/bao/uploaded/i4/153241374/T21ql7XnFbXXXXXXXX_!!153241374.jpg','post_fee':'8.00','price':'49.00','score':5,'title':'2011最新款 特價依情百諾打底衫  新店開張促銷款打底衫','type':'fixed','volume':2},{'cid':1623,'delist_time':'2011-12-27 16:53:50','nick':'wangxiangdong0705','num_iid':13187398930,'pic_url':'http://img03.taobaocdn.com/bao/uploaded/i3/T1tJGIXbRnXXbAt7Z2_045051.jpg','post_fee':'10.00','price':'49.00','score':5,'title':'2011最新款女裝 秋冬毛線時尚小短裙 特價包郵款短裙','type':'fixed','volume':1}]},'total_results':27}}]";
		discountJson2StrByType(name,"num_iid");
		
	}
	/**
	 * 根據傳入的json字串和型別返回折扣字串
	 * @param json
	 * @param type
	 * @return
	 */
	public static final void discountJson2StrByType(String json,String type) {
		List<String> list = new ArrayList<String>();
		try {
			JSONArray jsons = JSONArray.fromObject(json);
			for (int i=0;i<jsons.size();i++) {
				JSONObject jobj = jsons.getJSONObject(i);
				String a = jobj.toString();
				System.out.println(a);
				String b[] = a.split(",");
				for (int j = 0; j < b.length; j++) {
					if(b[j].indexOf(type)>0){
						String d[] = b[j].split(":");
						 list.add(d[1]);
					}
				}
			}
			//去重複資料
			Set<String> set = new HashSet<String>(list);
			for (Iterator<String> iterator = set.iterator(); iterator.hasNext();) {
				Object object = (Object) iterator.next();
				System.out.println(object);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
 	}