当前位置: 代码迷 >> J2EE >> request.setAttribute()怎么将xml文件设置进去
  详细解决方案

request.setAttribute()怎么将xml文件设置进去

热度:368   发布时间:2016-04-22 02:42:49.0
request.setAttribute()如何将xml文件设置进去
今天上司让用struts做个东西,查询某张表的全部数据,以xml的形式,返回全部数据,请问这是什么意思?

我以前只是使用xml文件用来配置什么的

这个方法就是写了一个xml文件,但如何将xml文件设置进去?
Java code
    public void BuildXML() throws Exception {        Element root, student, number, name, age;        root = new Element("student-info"); // 生成根元素:student-info        student = new Element("student"); // 生成元素:student(number,name,age)        number = new Element("number");        name = new Element("name");        age = new Element("age");        Document doc = new Document(root); // 将根元素植入文档doc中        number.setText("001");        name.setText("zhang");        age.setText("24");                student.addContent(name); // 先add的name,那么在XML文件中<name></name>出现在前面        student.addContent(number);                student.addContent(age);        root.addContent(student);        Format format = Format.getCompactFormat();        format.setEncoding("UTF-8"); // 设置xml文件的字符为gb2312        format.setIndent(" "); // 设置xml文件的缩进为4个空格        XMLOutputter XMLOut = new XMLOutputter(format);// 元素后换行一层元素缩四格        XMLOut.output(doc, new FileOutputStream("c:\\studentinfo.xml"));    }


------解决方案--------------------
可以先将从数据库中查出的结果保存在List中,然后循环这个List,将值set进去就可以了,O(∩_∩)O哈哈~
  相关解决方案