我有个xml类型的字符串 怎么生成生成xml文件

解决方案 »

  1.   

    有字符串?如果你的解析包有存成文件的方法,就直接调。如果没有,直接用java.io.File写文件啊。简单之至。public static void saveFile(String fileName, String str, boolean isAppend)
    throws Exception {
    File f = new File(fileName);
    BufferedWriter bw = null;
    try {
    if (!f.exists()) {
    if (!f.createNewFile()) {
    throw new IOException("file create failure...");
    }
    }
    bw = new BufferedWriter(new FileWriter(f, isAppend));
    bw.write(str); } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    bw.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  2.   

    public class XmlCreator
    {
    private String xml = "";

    public XmlCreator()
    {
    this.xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    }

    public XmlCreator(String Encoding)
    {
    this.xml = "<?xml version=\"1.0\" encoding=\"" + Encoding + "\"?>";
    }

    public void beginElement(String element)
    {
    this.xml += "<" + element + ">" ;
    }

    public void addAttribute(String attribute, String value)
    {
    this.xml = this.xml.substring(0,this.xml.length()-1) + " " + attribute + "='" + value + "'>";
    }

    public void endElement(String element)
    {
    this.xml += "</" + element + ">";
    } public void addElement(String element, String content)
    {
    this.xml += "<" + element + ">" + content + "</" + element + ">";
    }

    public void addElement(String element, int content)
    {
    this.xml += "<" + element + ">" + String.valueOf(content) + "</" + element + ">";
    }

    public String getXml()
    {
    return this.xml;
    }
    }
      

  3.   

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws IOException
    {
    String file = "";

    if(request.getParameter("file") != null && request.getParameter("file").trim() != "")
    {
    file = request.getParameter("file"); //从参数取XML文件名
    //组织XML文档路径
    String filePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/" + file;
    FileInputStream fileinputstream = new FileInputStream(filePath);//读取XML文件内容
    int lenght = fileinputstream.available();
    byte bytes[] = new byte[lenght];
    fileinputstream.read(bytes);
    fileinputstream.close(); String content = new String(bytes); //取出构建好的XML文档
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/xml");

    PrintWriter out = response.getWriter();
    out.write(content);
    out.flush();
    out.close();
    }
    return null;
    }
      

  4.   

    /**
     * 从xml脚本解析成Document对象
     * 
     * @param xmlStr
     *            String
     * @return Document
     */
    public static Document str2Doc(String xmlStr) throws IOException,
    JDOMException {
    InputStream inputStream = new ByteArrayInputStream(xmlStr
    .getBytes("UTF-8"));
    SAXBuilder saxBuilder = new SAXBuilder();
    Document doc = saxBuilder.build(inputStream);
    return doc;
    }