try {
Source source = new DOMSource(doc);
Result result = new StreamResult(w);
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
return SUCCESS;
}
public String writeXMLFile(String outfile,int companyid) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = dbf.newDocumentBuilder();
} catch (Exception e) {
}
Document doc = builder.newDocument();
Element root = doc.createElement("Picture");
doc.appendChild(root); // 将根元素添加到文档上
// 获取学生信息
//System.out.println("students_Vector.size:"+students_Vector.size());
for (int i = 0; i
if(s.getCompanyid()==companyid){
// 创建一个学生
Element stu= doc.createElement("item");
stu.setAttribute("PicturePath", s.getPath());
root.appendChild(stu);// 添加属性
// 创建文本姓名节点
Element name = doc.createElement("descriable");
stu.appendChild(name);
Text tname = doc.createTextNode(s.getDes());
name.appendChild(tname);
// 创建文本年龄节点
Element url = doc.createElement("url");
stu.appendChild(url); // 将age添加到学生节点上
Text tage = doc.createTextNode(s.getUrl());
url.appendChild(tage); // 将文本节点放在age节点上
}
}
try {
FileOutputStream fos = new FileOutputStream(outfile);
OutputStreamWriter outwriter = new OutputStreamWriter(fos);
// ((XmlDocument)doc).write(outwriter); //出错!
callWriteXmlFile(doc, outwriter, "UTF-8");
outwriter.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}