import java.lang.String;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import org.apache.axis2.AxisFault;
import org.dom4j.*;
public class User
{
//将一段XML字符串,转换成表格形式的HTML语言
public static String OutPutinTable(String xml, String trclass, String tdclass)
{
//输出结果的html
String table = "";
//输出表格头部的html
String tablehead = "";
try
{
//读入字符串类型的xml,并且转换为Document
Document doc = DocumentHelper.parseText(xml);
//得到xml根节点
//Element root = doc.getRootElement();
//获得根元素的子节点列表
//System.out.println(dom.getNode(doc, "/Dataset/Table/ID").getText());
//取得所有Table的位置
List l = doc.selectNodes("/Dataset/Table");
Iterator headiter = l.iterator();
Iterator contentiter = l.iterator();
//取表头部分:从第一个Table中取得所有的列头
Element headtmp = (Element) headiter.next();
List headtd = headtmp.elements();
Iterator headtditer = headtd.iterator();
tablehead = "<tr"+" class="+trclass+" >";
//把第一个Table中的所有元素名称存放到tablehead中
while (headtditer.hasNext())
{
Element headtmpp = (Element) headtditer.next();
tablehead += "<td"+" class="+tdclass+" >" + headtmpp.getName() + "</td>";
//System.out.println(tmp.asXML()+"/n");
}
tablehead += "</tr>";
//取xml中的数据作为表格内数据
//各个Table中循环得到元素值
while (contentiter.hasNext())
{
Element tmp = (Element) contentiter.next();
List td = tmp.elements();
Iterator tditer = td.iterator();
table += "<tr"+" class="+trclass+" >";
//Table中得到各个元素的值
while (tditer.hasNext())
{
Element tmpp = (Element) tditer.next();
table += "<td"+" class="+tdclass+" >" + tmpp.getText() + "</td>";
}
//System.out.println(tmp.asXML()+"/n");
table += "</tr>/n";
}
table = tablehead + "/n" + table;
}
catch (DocumentException e)
{
e.printStackTrace();
}
return table;
}
public static void main(String[] args)
{
String xml = "<Dataset> <Table> <ID>1</ID> <ServiceID>0</ServiceID> <ServiceName>Basic</ServiceName> <Memo>Basic</Memo> </Table> <Table> <ID>2</ID> <ServiceID>1</ServiceID> <ServiceName>SMS</ServiceName> <Memo>SMS Notification</Memo> </Table> <Table> <ID>3</ID> <ServiceID>2</ServiceID> <ServiceName>VoIP</ServiceName> <Memo>VoIP</Memo> </Table> <Table> <ID>4</ID> <ServiceID>3</ServiceID> <ServiceName>DiskSpace</ServiceName> <Memo>Large Disk Space</Memo> </Table> </Dataset>";
System.out.println(User.OutPutinTable(xml, "trclass", "tdclass"));
}
}