如何将List的数据保存到XML中求代码!!!!!!!!!

解决方案 »

  1.   

    PrintWriter out = response.getWriter();
    StringBuffer strBuffer = new StringBuffer();
                            strBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                            strBuffer.append("<root>");
                            strBuffer.append("<child>");
                            strBuffer.append("<child>");
                            strBuffer.append("<\root>");
    out.print(strBuffer.toString());
    out.flush();
    out.close();
      

  2.   

    可以具体点吗?
    我的数据是这样的
    List<bean> list=返回List的方法();
    谢谢!!!!!!!!!!!
      

  3.   

    public void createXMLFile(String filePath, List<String> list) {
    Document document = DocumentHelper.createDocument();
    Element element = document.addElement("content");
    if (list != null && !list.isEmpty()) {
    Element baseElement = element.addElement("base64Content");
    for (int i = 0; i < list.size(); i++) {
    Element decoderElement = baseElement.addElement("decoder");
    decoderElement.addAttribute("show", String.valueOf(i));
    decoderElement.setText(list.get(i));
    }
    }
    try {
    OutputFormat outFmt = new OutputFormat("\t", true); 
        outFmt.setEncoding("UTF-8"); 
        XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), outFmt);
    writer.write(document);
    writer.close();
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    用dom4j写的,jar包你自己找吧,这里不能发
      

  4.   

    这东西还要自己写啊,Java最大的好处就是库多,只要找找什么样的都有。自己写拼接麻烦容易错误而且还要对特殊字符& < 之类的转译。推荐使用框架:
    用Jaxb或者XStream,Simple。
    http://www.blogjava.net/xmatthew/archive/2008/12/10/245558.html
    推荐使用XSTream小巧好用,一句就可以了。http://xstream.codehaus.org/
      

  5.   

    List list=new ArrayList();
       Document doc=DocumentHelper.createDocument();
       Element root=doc.addElement("root");
       for(int i=0;i<list.size();i++){
       String temp=(String) list.get(i);
       Element value=root.addElement("value");
       value.addText(temp);
       }
      doc.asXML();
    再用流的方式保存物理文件就好了,需要加载dom4的jar包
      

  6.   

    public static void ListToXml(List list) throws IOException {
    doc = DocumentHelper.createDocument();
    doc.setXMLEncoding("UTF-8");
    Element root = doc.addElement("员工");
    for(Person person:(List<Person>)list){
    Element child = root.addElement(person.getName());
    child.addAttribute("员工ID", person.getId());
    child.addAttribute("年龄", person.getAge()+"");
    child.addAttribute("员工职位", person.getJob());
    child.addAttribute("薪资", person.getSalary()+"");
    }
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(
    new FileWriter(new File("e:\\dom.xml")), format);
    writer.write(doc);
    writer.close();
    }
      

  7.   

    什么格式的xml
    package list2xml;import org.dom4j.io.SAXReader;
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.XMLWriter;
    import java.io.File;
    import java.io.FileWriter;import java.util.List;
    import java.util.ArrayList;
    public class list2xml {    private String path;//文件路径    public List<String> openFile(String path) {
            List list = null;
            this.path = path;
            ArrayList<String> returnlist = new ArrayList<String>();
            try {
                SAXReader saxReader = new SAXReader();
                Document document = saxReader.read(new File(path));
                // 填充数据
                list = document.selectNodes("/do4j/list/data");
                for (Object el : list) {
                    returnlist.add(((Element) el).getText());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return (List<String>) returnlist;
        }    public List<String> openFile(String path, List userlist) {
            this.path = path;
            List list = null;
            ArrayList<String> returnlist = new ArrayList<String>();
            try {
                SAXReader saxReader = new SAXReader();
                Document document = saxReader.read(new File(path));
                // 填充数据
                list = document.selectNodes("/do4j/list/data");
                for (Object el : list) {
                    returnlist.add(((Element) el).getText());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            int i = 0;
            for (Object oo : userlist) {
                setString(oo, returnlist.get(i++));
            }
            return (List) returnlist;
        }    public void saveList(String path, List list) {
            this.path = path;
            Document document = DocumentHelper.createDocument();
            Element do4jElement = document.addElement("do4j");
            Element adminclassElement = do4jElement.addElement("adminclass");
            adminclassElement.setText("list2xml");
            Element listclassElement = do4jElement.addElement("list");
            Element dataElement;
            for (int i = 0; i < list.size(); ++i) {
                dataElement = listclassElement.addElement("data");
                dataElement.addAttribute("id", "" + i);
                dataElement.setText(getString(list.get(i)));
            }
            try {
                XMLWriter writer = new XMLWriter(new FileWriter(new File(path)));
                writer.write(document);
                writer.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }    public String getString(Object i) {
            return (String) i;
        }//获得字符串    public void setString(Object i, String s) {
            s = (String) i;
        }//获得字符串
    }