前两天遇到过一个问题,有如下的xml文件:
<rowdata>
<row>
<CAPTION>库存现金</CAPTION>
<ITEMLEVEL>1</ITEMLEVEL>
<POSITION>70010001</POSITION>
</row>
<row>
<CAPTION>银行存款</CAPTION>
<ITEMLEVEL>1</ITEMLEVEL>
<POSITION>70020001</POSITION>
</row>
<row>
<CAPTION>银行存款\工商银行</CAPTION>
<ITEMLEVEL>2</ITEMLEVEL>
<POSITION>7002000100010001</POSITION>
</row>
<row>
<CAPTION>银行存款\农业银行</CAPTION>
<ITEMLEVEL>2</ITEMLEVEL>
<POSITION>7002000100020001</POSITION>
</row>
</rowdata>
要求如下:
将数据整理生成有层级关系的对象。
1. 数据关系说明:XML中根据ITEMLEVEL的层级,查找上级的方法,POSITION每八位代表一级,如果ITEMLEVEL=3,上级就是该对象POSION的前16位相同的对象。
2. 生成后对象第1级包含子对象是2级,2级包含子表象是3级,3级包含4级,如此类推。
有多种工具包可以解析xml文件生成相应的pojo集合类,所以xml解析过程不做探讨,代码如下:
public class DataRowPojo {
private String caption;
private String itemLevel;
private String position;
private String parentPosition;
List<DataRowPojo> childPojos;
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getItemLevel() {
return itemLevel;
}
public void setItemLevel(String itemLevel) {
this.itemLevel = itemLevel;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public List<DataRowPojo> getChildPojos() {
return childPojos;
}
public void setChildPojos(List<DataRowPojo> childPojos) {
this.childPojos = childPojos;
}
public String getParentPosition() {
int parentLevel = new Integer(this.itemLevel).intValue()- 1;
int size = (parentLevel) > 0 ? parentLevel : 0;
return this.position.substring(0, size*8);
}
public void setParentPosition(String parentPosition) {
this.parentPosition = parentPosition;
}
public String toString() {
return "levle: " +this.itemLevel+"\t"+"caption: "+this.caption+"\t"+
"position:"+this.position +"this.parentPosotion:"+this.parentPosition;
}
}
树构建类:
public class TreeBuild {
public static List<DataRowPojo> getRowPojos() {
List<DataRowPojo> pojos = null;
try {
String xml = SoawareIoUtil.loadFromFileAsString("data.xml");
pojos = ParseData.parse(xml);
} catch (Exception e) {
throw new RuntimeException("数据解析失败",e);
}
return pojos;
}
public static Map<String,List<DataRowPojo>> groupPojo(List<DataRowPojo> pojos) {
Map<String,List<DataRowPojo>> map = new HashMap<String,List<DataRowPojo>>();
for(int i=0;i<pojos.size();i++) {
DataRowPojo pojo = pojos.get(i);
String level = pojo.getItemLevel();
if(map.get(level) == null) {
map.put(level,new ArrayList<DataRowPojo>());
}
map.get(level).add(pojo);
}
return map;
}
/***
* 为了提升遍历效率,必须要减小遍历集合,用map将集合进行分层
*
* */
public static void buildTree(List<DataRowPojo> parentPojos,
Map<String,List<DataRowPojo>> pojoMap) {
int parentLevle = new Integer(parentPojos.get(0).getItemLevel()).intValue();
int childLevle = parentLevle + 1;
List<DataRowPojo> childPojos = pojoMap.get(new Integer(childLevle).toString());
if(childPojos == null) {
return;
}
List<DataRowPojo> record = new ArrayList<DataRowPojo>();
for(Iterator<DataRowPojo> parentIt = parentPojos.iterator();
parentIt.hasNext();) {
DataRowPojo pojo = parentIt.next();
for(Iterator<DataRowPojo> childIt = childPojos.iterator();
childIt.hasNext();) {
DataRowPojo childPojo = childIt.next();
if(childPojo.getParentPosition().equals(pojo.getPosition())) {
if(pojo.getChildPojos()==null) {
pojo.setChildPojos(new ArrayList<DataRowPojo>());
}
pojo.getChildPojos().add(childPojo);
record.add(childPojo);
childIt.remove();
}
}
}
boolean isChildExists = (pojoMap.get(new Integer(childLevle+1).toString())==null)? false : true;
if(!isChildExists) {
return ;
}else{
buildTree(record, pojoMap);
}
}
private static DataRowPojo buildRoot() {
DataRowPojo root = new DataRowPojo();
root.setItemLevel("0");
root.getChildPojos().addAll(groupPojo(getRowPojos()).get("1"));
return root;
}
public static void main(String[] args) {
DataRowPojo pojo = buildRoot();
long starttime = System.currentTimeMillis();
buildTree(pojo.getChildPojos(),groupPojo(getRowPojos()));
System.out.println(pojo.getChildPojos().size());
System.out.println("消耗的时间为:" + (System.currentTimeMillis()-starttime) +"ms");
}
}
<rowdata>
<row>
<CAPTION>库存现金</CAPTION>
<ITEMLEVEL>1</ITEMLEVEL>
<POSITION>70010001</POSITION>
</row>
<row>
<CAPTION>银行存款</CAPTION>
<ITEMLEVEL>1</ITEMLEVEL>
<POSITION>70020001</POSITION>
</row>
<row>
<CAPTION>银行存款\工商银行</CAPTION>
<ITEMLEVEL>2</ITEMLEVEL>
<POSITION>7002000100010001</POSITION>
</row>
<row>
<CAPTION>银行存款\农业银行</CAPTION>
<ITEMLEVEL>2</ITEMLEVEL>
<POSITION>7002000100020001</POSITION>
</row>
</rowdata>
要求如下:
将数据整理生成有层级关系的对象。
1. 数据关系说明:XML中根据ITEMLEVEL的层级,查找上级的方法,POSITION每八位代表一级,如果ITEMLEVEL=3,上级就是该对象POSION的前16位相同的对象。
2. 生成后对象第1级包含子对象是2级,2级包含子表象是3级,3级包含4级,如此类推。
有多种工具包可以解析xml文件生成相应的pojo集合类,所以xml解析过程不做探讨,代码如下:
public class DataRowPojo {
private String caption;
private String itemLevel;
private String position;
private String parentPosition;
List<DataRowPojo> childPojos;
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getItemLevel() {
return itemLevel;
}
public void setItemLevel(String itemLevel) {
this.itemLevel = itemLevel;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public List<DataRowPojo> getChildPojos() {
return childPojos;
}
public void setChildPojos(List<DataRowPojo> childPojos) {
this.childPojos = childPojos;
}
public String getParentPosition() {
int parentLevel = new Integer(this.itemLevel).intValue()- 1;
int size = (parentLevel) > 0 ? parentLevel : 0;
return this.position.substring(0, size*8);
}
public void setParentPosition(String parentPosition) {
this.parentPosition = parentPosition;
}
public String toString() {
return "levle: " +this.itemLevel+"\t"+"caption: "+this.caption+"\t"+
"position:"+this.position +"this.parentPosotion:"+this.parentPosition;
}
}
树构建类:
public class TreeBuild {
public static List<DataRowPojo> getRowPojos() {
List<DataRowPojo> pojos = null;
try {
String xml = SoawareIoUtil.loadFromFileAsString("data.xml");
pojos = ParseData.parse(xml);
} catch (Exception e) {
throw new RuntimeException("数据解析失败",e);
}
return pojos;
}
public static Map<String,List<DataRowPojo>> groupPojo(List<DataRowPojo> pojos) {
Map<String,List<DataRowPojo>> map = new HashMap<String,List<DataRowPojo>>();
for(int i=0;i<pojos.size();i++) {
DataRowPojo pojo = pojos.get(i);
String level = pojo.getItemLevel();
if(map.get(level) == null) {
map.put(level,new ArrayList<DataRowPojo>());
}
map.get(level).add(pojo);
}
return map;
}
/***
* 为了提升遍历效率,必须要减小遍历集合,用map将集合进行分层
*
* */
public static void buildTree(List<DataRowPojo> parentPojos,
Map<String,List<DataRowPojo>> pojoMap) {
int parentLevle = new Integer(parentPojos.get(0).getItemLevel()).intValue();
int childLevle = parentLevle + 1;
List<DataRowPojo> childPojos = pojoMap.get(new Integer(childLevle).toString());
if(childPojos == null) {
return;
}
List<DataRowPojo> record = new ArrayList<DataRowPojo>();
for(Iterator<DataRowPojo> parentIt = parentPojos.iterator();
parentIt.hasNext();) {
DataRowPojo pojo = parentIt.next();
for(Iterator<DataRowPojo> childIt = childPojos.iterator();
childIt.hasNext();) {
DataRowPojo childPojo = childIt.next();
if(childPojo.getParentPosition().equals(pojo.getPosition())) {
if(pojo.getChildPojos()==null) {
pojo.setChildPojos(new ArrayList<DataRowPojo>());
}
pojo.getChildPojos().add(childPojo);
record.add(childPojo);
childIt.remove();
}
}
}
boolean isChildExists = (pojoMap.get(new Integer(childLevle+1).toString())==null)? false : true;
if(!isChildExists) {
return ;
}else{
buildTree(record, pojoMap);
}
}
private static DataRowPojo buildRoot() {
DataRowPojo root = new DataRowPojo();
root.setItemLevel("0");
root.setCaption("元素父节点");
root.getChildPojos().addAll(groupPojo(getRowPojos()).get("1"));
return root;
}
public static void main(String[] args) {
DataRowPojo pojo = buildRoot();
long starttime = System.currentTimeMillis();
buildTree(pojo.getChildPojos(),groupPojo(getRowPojos()));
System.out.println(pojo.getChildPojos().size());
System.out.println("消耗的时间为:" + (System.currentTimeMillis()-starttime) +"ms");
}
}