下面代码实现了xml的document转换成string 类型,用于xml输出
/**
* write doc to log
* @param doc
* @throws IOException
*/
private static String writeXml(Document doc) throws IOException {
// write the document to output stream ("out")
ByteArrayOutputStream out = new ByteArrayOutputStream();;
OutputFormat format = new OutputFormat(doc);
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(doc.getDocumentElement());
InputStream is = new ByteArrayInputStream(out.toByteArray());
StringBuffer stringBuffer = new StringBuffer();
byte[] b = new byte[1024];
int len=0;
while( (len = is.read(b)) > -1 )
{
for(int i=0; i<len; i++)
{
stringBuffer.append((char)b[i]);
}
}
return stringBuffer.toString();
}