【问题标题】:xml string to filexml 字符串到文件
【发布时间】:2009-12-24 09:05:53
【问题描述】:

我希望将 xml 字符串转换为文件,为此我正在按照以下方式进行操作,

String xmlFile=responseXMLName;
log.info("xml file :" +xmlFile);
fr = new FileWriter(new File(xmlFile));
Writer br= new BufferedWriter(fr); 
log.info("respose string"+responseXMLString);
br.write(responseXMLString);
br.close();  

我想将xml文件数据传递给这个函数,我该怎么做?

Document doc = builder.build(...);

【问题讨论】:

    标签: java xml


    【解决方案1】:
    StringReader reader = new StringReader( s );
    InputSource inputSource = new InputSource( reader );
    Document doc = builder.parse( inputSource );
    reader.close();
    

    会成功的。

    【讨论】:

      【解决方案2】:

      如果你想要一个文件:

      FileWriter fr = null;
      try {
        String xmlFile=responseXMLName;
        log.info("xml file :" +xmlFile);
        fr = new FileWriter(xmlFile);
        log.info("respose string"+responseXMLString);
        fr.write(responseXMLString);
      } finally {
        if (fr != null) {
            fr.close();
        }
      }
      

      获取文档:

      StringReader reader = new StringReader( responseXMLString );
      InputSource inputSource = new InputSource( reader );
      Document doc = builder.parse( inputSource );
      reader.close();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        • 2011-07-24
        • 2011-02-03
        • 2022-01-18
        • 2017-10-19
        • 1970-01-01
        相关资源
        最近更新 更多