xml字符串到文件

时间:2009-12-24 09:05:53

标签: java xml

我希望将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(...);

2 个答案:

答案 0 :(得分:2)

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

会做到这一点。

答案 1 :(得分:0)

如果你想要一个文件:

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();