现在我有一个list,list里面是一个javaBean的集合
如:
OrgBean orgBean = new OrgBean();
orgBean.setorg_no="32";
orgBean.setorg_name="江苏";
orgBean.setp_org_no="0";
list.add(orgBean);OrgBean orgBean1 = new OrgBean();
orgBean1.setorg_no="3201";
orgBean1.setorg_name="南京";
orgBean1.setp_org_no="32";
list.add(orgBean1);OrgBean orgBean2 = new OrgBean();
orgBean2.setorg_no="320101";
orgBean2.setorg_name="xx区1";
orgBean2.setp_org_no="3201";
list.add(orgBean2);OrgBean orgBean3 = new OrgBean();
orgBean3.setorg_no="3202";
orgBean3.setorg_name="苏州";
orgBean3.setp_org_no="32";
list.add(orgBean3);OrgBean orgBean4 = new OrgBean();
orgBean4.setorg_no="320102";
orgBean4.setorg_name="xx区2";
orgBean.setp_org_no="0";
list.add(orgBean4);根据这个list我需要生成一个xml,类似于树的结构
如下:
<List>
<Org org_no="32" org_name="江苏">
<Org org_no="3201" org_name="南京">
<Org org_no="320101" org_name="xx区1"/>
<Org org_no="320101" org_name="xx区2"/>
</Org>
<Org no_no="3202" org_name="苏州"/>
</Org>
</List>想请教高手给个算法,递归本人非常不熟悉,
希望能给个详细的代码,谢谢!
如:
OrgBean orgBean = new OrgBean();
orgBean.setorg_no="32";
orgBean.setorg_name="江苏";
orgBean.setp_org_no="0";
list.add(orgBean);OrgBean orgBean1 = new OrgBean();
orgBean1.setorg_no="3201";
orgBean1.setorg_name="南京";
orgBean1.setp_org_no="32";
list.add(orgBean1);OrgBean orgBean2 = new OrgBean();
orgBean2.setorg_no="320101";
orgBean2.setorg_name="xx区1";
orgBean2.setp_org_no="3201";
list.add(orgBean2);OrgBean orgBean3 = new OrgBean();
orgBean3.setorg_no="3202";
orgBean3.setorg_name="苏州";
orgBean3.setp_org_no="32";
list.add(orgBean3);OrgBean orgBean4 = new OrgBean();
orgBean4.setorg_no="320102";
orgBean4.setorg_name="xx区2";
orgBean.setp_org_no="0";
list.add(orgBean4);根据这个list我需要生成一个xml,类似于树的结构
如下:
<List>
<Org org_no="32" org_name="江苏">
<Org org_no="3201" org_name="南京">
<Org org_no="320101" org_name="xx区1"/>
<Org org_no="320101" org_name="xx区2"/>
</Org>
<Org no_no="3202" org_name="苏州"/>
</Org>
</List>想请教高手给个算法,递归本人非常不熟悉,
希望能给个详细的代码,谢谢!
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;public class TxtToXml {
private String strTxtFileName; private String strXmlFileName; public TxtToXml() {
strTxtFileName = new String();
strXmlFileName = new String();
} public void createXml(String strTxt, String strXml) {
strTxtFileName = strTxt;
strXmlFileName = strXml;
String strTmp;
try {
BufferedReader inTxt = new BufferedReader(new FileReader(
strTxtFileName));
BufferedWriter outXml = new BufferedWriter(new FileWriter(
strXmlFileName));
outXml.write("<?xml version= \"1.0\" encoding=\"gb2312\"?>");
outXml.newLine();
outXml.write("<people>");
while ((strTmp = inTxt.readLine()) != null) {
StringTokenizer strToken = new StringTokenizer(strTmp, ",");
String arrTmp[];
arrTmp = new String[3];
for (int i = 0; i < 3; i++)
arrTmp[i] = new String("");
int index = 0;
outXml.newLine();
outXml.write(" <students>");
while (strToken.hasMoreElements()) {
strTmp = (String) strToken.nextElement();
strTmp = strTmp.trim();
arrTmp[index++] = strTmp;
}
outXml.newLine();
outXml.write(" <name>" + arrTmp[0] + "</name>");
outXml.newLine();
outXml.write(" <sex>" + arrTmp[1] + "</sex>");
outXml.newLine();
outXml.write(" <age>" + arrTmp[2] + "</age>");
outXml.newLine();
outXml.write(" </students>");
}
outXml.newLine();
outXml.write("</people>");
outXml.flush();
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
String txtName = "testtxt.txt";
String xmlName = "testxml.xml";
TxtToXml thisClass = new TxtToXml();
thisClass.createXml(txtName, xmlName);
}
}
上面代码是读取txt文件,转换成xml文件的实现,把读到的内容,改成楼主的list可以吧!希望有帮助
实现过程
1.定义你的xml的xsd
2.根据定义的xsd生成相应的bean类
3.构建你的bean类的对象
4.利用类似如下代码写到xml中 Page page = new Page();
Marshaller marshall = jc.createMarshaller();
FileWriter ff = new FileWriter("c:\\tt.txt");
marshall.marshal(page, ff);
ff.close();
while(list.size()>0){
OrgBean bean=(OrgBean)list.get(i);
int p=sortList.exsit(bean.getP_org_no();
if(p!=-1 || bean.getP_org_no()==0){
sortList.add(p+1,bean);
list.remove(bean);
//找到了,移除当前的,后面的下标自动-1
}else{
i++;//没找到,下标+1
}
}private int exsit(String parentid){
//循环sortList,看这个parentid是不是已经放在新集合里了,如果不是则返回-1,找到则范围序号
}