java代码导出xml java代码导出mongodb数据

JAVA获取XML文件并输出// 给你一个例子,你参考下
// java通过dom读写xml文件
/*要读的xml文件
?xml version="1.0" encoding="GB2312"?
学生花名册
学生 性别 = "男"
姓名李华/姓名
年龄14/年龄
/学生
学生 性别 = "男"
姓名张三/姓名
年龄16/年龄
/学生
/学生花名册
*/
package xml;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class DomTest {
Vector students_Vector;
private Vector readXMLFile(String file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(file); // 获取到xml文件
// 下面开始读取
Element root = doc.getDocumentElement(); // 获取根元素
NodeList students = root.getElementsByTagName_r("学生");
students_Vector = new Vector();
for (int i = 0; istudents.getLength(); i++) {
// 一次取得每一个学生元素
Element ss = (Element) students.item(i);
// 创建一个学生的实例
student stu = new student();
stu.setSex(ss.getAttribute("性别"));
NodeList names = ss.getElementsByTagName_r("姓名");
Element e = (Element) names.item(0);
Node t = e.getFirstChild();
stu.setName(t.getNodeValue());
NodeList ages = ss.getElementsByTagName_r("年龄");
e = (Element) ages.item(0);
t = e.getFirstChild();
stu.setAge(Integer.parseInt(t.getNodeValue()));
students_Vector.add(stu);
}
return students_Vector;
}
// 写入xml文件
public static void callWriteXmlFile(Document doc, Writer w, String encoding) {
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();
}
}
private void writeXMLFile(String outfile) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = dbf.newDocumentBuilder();
} catch (Exception e) {
}
Document doc = builder.newDocument();
Element root = doc.createElement("学生花名册");
doc.appendChild(root); // 将根元素添加到文档上
// 获取学生信息
for (int i = 0; istudents_Vector.size(); i++) {
student s = (student) students_Vector.get(i);
// 创建一个学生
Element stu = doc.createElement("学生");
stu.setAttribute("性别", s.getSex());
root.appendChild(stu);// 添加属性
// 创建文本姓名节点
Element name = doc.createElement("姓名");
stu.appendChild(name);
Text tname = doc.createTextNode(s.getName());
name.appendChild(tname);

推荐阅读