for循环三层寻换成xml数据格式字符串

 

public class SortAction extends ActionSupport {
	String xml1 = "";
	String xml2 = "";
	String xml3 = "";
	List<Sort> list1 = null;
	List<Sort> list2 = null;
	List<Sort> list3 = null;
	private BookSortService bookSortService;
	public void setBookSortService(BookSortService bookSortService) {
		System.out.println("-->SortAction.setBookSortService()");
		this.bookSortService = bookSortService;
	}

	public String sortList(){
			list1 = this.bookSortService.queryFirstBookSort();
			
			// 循环输出一级图书分类
			for (Sort sort1 : list1) {
				list2 = this.bookSortService.queryBookSortByNum(sort1.getNumbers());
				
				// 循环输出二级图书分类
				for (Sort sort2 : list2) {
					list3 = this.bookSortService.queryBookSortByNum(sort2.getNumbers());	
					
					//循环输出三级图书分类
					for (Sort sort3 : list3) {
						xml3 += "<third name=\""+sort3.getName() +" \"></third>";
					}	
					xml2 += "<second name=\""+sort2.getName() +" \">"+xml3+"</second>";
					xml3 = "";
				}
				xml1 += "<first name=\""+sort1.getName() +" \">"+xml2+"</first>";
				xml2 = "";
			}
		return xml1;
	}
}
效果如下:

<first name="政治学 ">
	<second name="哲学 ">
		<third name="男人帮 "></third>
		<third name="倚天屠龙 "></third>
		<third name="神雕侠侣 "></third>
	</second>
	<second name="社科总论 "></second>
	<second name="政治 "></second>
	<second name="法律 "></second>
	<second name="军事 "></second>
	<second name="财经管理 "></second>
	<second name="工商管理 "></second>
	<second name="历史 "></second>
</first>
<first name="法律类 ">
	<second name="想想学 "></second>
	<second name="纯粹学 "></second>
</first>
<first name="哲学类 "></first>
<first name="经济学与经济管理类 "></first>
<first name="文学与艺术类 "></first>
<first name="教育类 "></first>
<first name="工业技术 "></first>
<first name="历史类 "></first>
<first name="文化类 "></first>
<first name="信息类 "></first>
<first name="传播类 "></first>

 

你可能感兴趣的:(for循环)