jDom生成xml文件
1.导入对应的包
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
- 代码
/**
* 得到xml文件
*
* @param beanList
* @return String
*/
private String getXml(List<FeildReturnMap> beanList,TableInfo info) throws Exception{
String result = "";
//跟节点名称
Element root = new Element("result");
Document doc = new Document(root);
List<String> externalFields = info.getExternalFields();
for (int i = 0; i < beanList.size(); i++) {
//子节点名称
Element element = new Element("item");
//单个项的名称和值
element.addContent(new Element("userName").setText("sbsbsb"));
//
root.addContent(element);
}
XMLOutputter XMLOut = new XMLOutputter();
result = XMLOut.outputString(doc);
return result;
}
- 结果